How to fire event if CSS class is changed using jQuery ? Last Updated : 29 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Given an HTML document with heading and button, jQuery should fire an event when the CSS class of the button changes. Approach: In jQuery, there is no provision to trigger an event on class change. An alternative method is to manually raise an event when you programmatically change the class using the trigger() function. The trigger() function will raise an event whenever the class of the button is changed. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> .buttonstyle { background-color: #4CB96B; color: white; } .buttonsize { padding: 1em; height: 70px; width: 400px; border: .5px solid black; outline: none; font-size: large; border-radius: 10px; } </style> </head> <body> <center> <h1> Click the button to change its style </h1> <button id="classchange-btn" class="buttonsize"> Click Me </button> </center> <script> $("#classchange-btn").click(function () { $(this).toggleClass("buttonstyle") .trigger('classChanged'); }); $("#classchange-btn").on( "classChanged", function () { alert("Button Style Change event fired"); }); </script> </body> </html> Output: Initially the page looked like the following image. On clicking the button, an alert message pops up indicating that the listener was fired and the CSS class of the button has changed. After closing the popup, the button's CSS class changed (notice the green background color and white text). jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples. Comment More infoAdvertise with us Next Article How to fire event if CSS class is changed using jQuery ? S soorajsnair Follow Improve Article Tags : Web Technologies JQuery jQuery-Questions Similar Reads How to check an element contains a class using jQuery? Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not. This can be used to check for multiple classes. Syntax: 2 min read How to run the code on change event using jQuery ? In this article, we will see how to run the code on change events using jQuery. The change event is used to trigger when the user changes the value of the element. Syntax: $(selector).change(function) Example: In the below example, we are creating a textarea element that contains GeeksforGeeks text. 1 min read How to add and remove CSS classes to an element using jQuery ? In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(clas 2 min read How to fire an event on file select using jQuery ? Given an HTML document and the task to fire an event when a file is selected using jQuery. The JQuery change() method is used to fire an event when file is selected. Using change() method: It is used to change the value of the input fields. This method works with "input, textarea and select" element 2 min read How to find the class of clicked element using jQuery ? In this article, we will find the class of the clicked element using jQuery. To find the class of clicked element, we use this.className property. The className property is used to set or return the value of an elementâs class attribute. Using this property, the user can change the class of an eleme 1 min read Like