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 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 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 go to link cialis generisch bestellen buy viagra no prescription cialis la paloma ranchettes ktora viagra does accutane help scars essay on reality of women rights 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" />	

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;
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


Lightning Aura Components Developer Guide

Leave a Reply