Add And Remove Styles on Lightning Components

It is possible to add and remove styles on Lightning components or element during runtime.

To retrieve the class name on a component, use  herbal viagra suppliers https://albionfoundation.org/perpill/levitra-istruzioni-per-luso/63/ who is the black guy in the cialis commercial companies in canada that ship male enhancement s entro quanto tempo fa effetto il viagra how do i set up an email account on my iphone 8 https://eagfwc.org/men/can-you-purchase-viagra-online/100/ danger viagra generique didnt do my homework yahoo tome cialis y me duele la cabeza follow url go to link go essays on imperialism in asia spanish essay about free time go to site essay contract fasigyn and flagyl medication https://www.cuea.edu/cueapress/?paper=intros-for-essays-examples go to link https://earthwiseradio.org/editing/presidential-vs-parliamentary-system-of-government-essay/8/ cialis generisch bestellen buy viagra no prescription cialis la paloma ranchettes https://dsaj.org/buyingmg/sildenafil-100mg-how-to-take/200/ ktora viagra does accutane help scars essay on reality of women rights https://eagfwc.org/men/comprar-viagra-en-farmacia-online/100/ https://hhkidsdentist.com/advising/viagra-for-men-who-dont-need-it/81/ here component.find(‘myCmpId’).get(‘v.class’), where myCmpId is the aura:id attribute value and the class is added dynamically. To append and remove CSS classes from a component or element, use the $A.util.addClass(cmpTarget, ‘class’) and $A.util.removeClass(cmpTarget, ‘class’) methods.

Consider this component – AddRemoveCSS.cmp

<aura:component >
  <div aura:id="changeBtn">Change My Style!</div><br />
  <lightning:button class ="btn" onclick="{!c.addCSS}" label="Add Style" />
    <lightning:button onclick="{!c.removeCSS}" label="Remove Style" />	
</aura:component>

I added the btn class to the Add Style button to style it a little bit. Check the style below in the STYLE code.

The controller:

({
	 addCSS: function(cmp, event) {
        var cmpTarget = cmp.find('changeBtn');
        $A.util.addClass(cmpTarget, 'changeStyle');
    },
    
    removeCSS: function(cmp, event) {
        var cmpTarget = cmp.find('changeBtn');
        $A.util.removeClass(cmpTarget, 'changeStyle');
    }
})

The STYLE code:

.THIS.changeStyle {
color: blue;
width: 150px;
margin-top: 20px;
margin-left: 15px;
}
.THIS.btn{
margin-right:10px;
margin-left: 15px;
}

The THIS.btn added both right and left margins to the first button to improve the user experience.

The result of clicking the Add Style button is attached below

Buttons to add and remove style

Reference

Lightning Aura Components Developer Guide

Leave a Reply