{"id":6206,"date":"2021-06-15T12:18:26","date_gmt":"2021-06-15T12:18:26","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6206"},"modified":"2021-06-15T12:18:29","modified_gmt":"2021-06-15T12:18:29","slug":"how-to-add-or-remove-css-style-from-the-component-element-during-runtime-or-dynamically","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/06\/15\/how-to-add-or-remove-css-style-from-the-component-element-during-runtime-or-dynamically\/","title":{"rendered":"How to Add or Remove CSS style from the component\/element during runtime or dynamically"},"content":{"rendered":"\n<p>In this blog, we will discuss how to Add and Remove CSS style from the component or element during runtime.<\/p>\n\n\n\n<p>To add and remove CSS style from the component or element, you need to use $A.util.addClass(object element ,string class) and $A.util.removeClass(object element, string class) methods, where \u201c<strong>element<\/strong>\u201d is the component to add and remove the CSS style from and \u201c<strong>class<\/strong>\u201d is the CSS class to be applied on the component.<\/p>\n\n\n\n<p>To find component, use component.find(&#8216;Cmp&#8217;),where Cmp is the aura:id attribute value.<\/p>\n\n\n\n<p>Let\u2019s take an example to add and remove style from the component or element during runtime<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em><span style=\"text-decoration: underline;\">Create Lightning Component<\/span><\/em><\/strong><\/h4>\n\n\n\n<p><strong>SampleComponent<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:component>    \r\n    &lt;div aura:id=\"Cmp\">SalesForce Lightning&lt;\/div>&lt;br\/>\r\n    &lt;lightning:button onclick=\"{!c.addCSS}\" label=\"Add Style\" \/>\r\n    &lt;lightning:button onclick=\"{!c.removeCSS}\" label=\"Remove Style\" \/>\r\n&lt;\/aura:component>\r<\/code><\/pre>\n\n\n\n<p><strong>SampleCss<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.THIS.changeStyle {\r\n    background-color:white;\r\n    color:black;\r\n}<\/code><\/pre>\n\n\n\n<p><strong>SampleController<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To add CSS style use $A.util.addClass(object element, string class) method and to remove CSS style use $A.util.removeClass(object element, string class) method.<\/li><li>Whenever the user clicks on \u201c<strong>Add style<\/strong>\u201d button the \u201c<strong>addclass\u201d<\/strong> controller method gets invoked and it will appends the CSS style to the component or element.<\/li><li>Similary,when the user clicks on \u201c<strong>Remove style<\/strong>\u201d button the \u201c<strong>removeclass<\/strong>\u201d controller method gets invoked and it will remove the CSS style from the component or element. Refer to the below code.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n    addCSS: function(cmp, event) {\r\n        var cmpDiv = cmp.find(\u2018Cmp\u2019);\r\n        $A.util.addClass(cmpDiv, 'changeStyle');\r\n    },\r\n    removeCSS: function(cmp, event) {\r\n        var cmpDiv = cmp.find(\u2018Cmp\u2019);\r\n        $A.util.removeClass(cmpDiv, 'changeStyle');\r\n    }  \r\n})<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Here is the output, so now when the user clicks on the \u201c<strong>Add style\u201d<\/strong> button the background and the text color of \u201c<strong>Salesforce Lightning<\/strong>\u201d text will change.<\/li><\/ul>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/06\/1_Add-Style.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/06\/1_Add-Style.png\" alt=\"Add Style during runtime\"><\/a><\/center>\n<font size=\"2\"><center><i>Add Style during runtime<\/i><\/center><\/font>\n\n\n\n<ul class=\"wp-block-list\"><li>Similarly, when user clicks on \u201c<strong>Remove style\u201d<\/strong> button the background and the text color of \u201c<strong>Salesforce Lightning<\/strong>\u201d text will be removed as shown in the below images.<\/li><\/ul>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/06\/2_Remove-style.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/06\/2_Remove-style.png\" alt=\"Remove style during runtime\"><\/a><\/center>\n<font size=\"2\"><center><i>Remove style during runtime<\/i><\/center><\/font>\n\n\n\n<p>This way you can add and\/or remove CSS style from the component or element. We hope you may find this blog resourceful and helpful.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/05\/29\/how-to-add-utility-bar-in-a-salesforce-lightning-application\/\" data-type=\"URL\" data-id=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/05\/29\/how-to-add-utility-bar-in-a-salesforce-lightning-application\/\" target=\"_blank\">How to add Utility bar in a Salesforce lightning application<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/04\/12\/how-to-notify-user-about-unsaved-changes-on-the-ui-using-lightning-component\/\" target=\"_blank\">How to Notify User about Unsaved Changes on the UI using Lightning Component<\/a> <\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/24\/add-delete-row-dynamically-in-salesforce-lightning\/\" target=\"_blank\">Add Delete Row Dynamically In Salesforce Lightning<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/22\/salesforce-how-to-get-row-index-of-lightning-table-rows\/\" target=\"_blank\">Salesforce \u2013 How to get row index of lightning table rows<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2020\/12\/30\/how-to-activate-and-add-salesforce-lightning-component-on-community-experience\/\" target=\"_blank\">How to Activate and Add Salesforce Lightning component on Community experience<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss how to Add and Remove CSS style from the component or element during runtime. To add and remove CSS style from the component or element, you need to use $A.util.addClass(object element ,string class) and $A.util.removeClass(object element, string class) methods, where \u201celement\u201d is the component to add and remove the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/06\/15\/how-to-add-or-remove-css-style-from-the-component-element-during-runtime-or-dynamically\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1155,1153,1158,1131,63,1157,1156,1055,1133,208,1154,367],"class_list":["post-6206","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-add-class","tag-add-remove-style","tag-add-remove-style-dynamically","tag-aura","tag-css","tag-css-lightning","tag-css-style","tag-lightning-aura-component","tag-lightning-basic","tag-lightning-component","tag-remove-class","tag-salesforce"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6206"}],"version-history":[{"count":3,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6206\/revisions"}],"predecessor-version":[{"id":6226,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6206\/revisions\/6226"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}