jQuery | Get and Set CSS Classes Last Updated : 27 Feb, 2019 Comments Improve Suggest changes Like Article Like Report jQuery has various methods for CSS manipulation which are listed below: addClass(): Adds one or more classes to the selected elements removeClass(): Removes one or more classes from selected elements toggleClass(): Toggles between adding or removing classes from selected elements css(): Sets or returns style attribute jQuery addClass() Method: The addClass is used to add more property to each selected element. It can also be used to change the property of the selected element. Syntax: $(content).addClass(target) Example: html <!DOCTYPE html> <html> <head> <title> jQuery addClass() Method </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- Script to use addClass method --> <script> $(document).ready(function() { $("button").click(function() { $("h1, h2, p").addClass("green"); $("div").addClass("abs"); }); }); </script> <style> .abs { font-weight: bold; font-size: xx-large; } .green { color:green; } </style> </head> <body> <h1>GFG</h1> <h2>GeeksForGeeks</h2> <p>This is gfg.</p> <div>This is some different text.</div><br> <button>Add classes to elements</button> </body> </html> Output: Before click on the button: After click on the button: jQuery removeClass() Method: This method is used to remove a specific class attribute from different elements. Syntax: $(content).removeClass(target) Example: html <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- Script to use removeClass method --> <script> $(document).ready(function() { $("button").click(function() { $("h1, h2, p").removeClass("green"); }); }); </script> <style> .important { font-weight: bold; font-size: xx-large; } .green { color:green; } </style> </head> <body> <h1 class="green">Heading 1</h1> <h2 class="green">GFG</h2> <p class="green">welcome to GeeksForGeeks.</p> <p>This is other paragraph.</p> <button>Remove class from elements</button> </body> </html> Output: Before click on the button: After click on the button: jQuery toggleClass() Method: This method toggles between adding or removing classes from selected elements. Syntax: $(content).toggleClass(target) Example: html <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- Script to use toggleClass() method --> <script> $(document).ready(function() { $("button").click(function() { $("h1, h2, p").toggleClass("green"); }); }); </script> <style> .green { color:lightgreen; } </style> </head> <body> <h1>Heading</h1> <h2>gfg</h2> <p>welcome to gfg</p> <p>This is other paragraph.</p> <button>Toggle class</button> </body> </html> Output: Before click on the button: After click on the button: jQuery css() Method: This method sets or returns one or more style properties for selected elements. Syntax: $(content).css(target) Example: html <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- Script to use css() method --> <script> $(document).ready(function() { $("button").click(function() { alert("Background color = " + $("p").css("background-color")); }); }); </script> </head> <body> <h2>This is a heading</h2> <p style="background-color:green;">This is a gfg.</p> <p style="background-color:lightgreen">This is a gfg.</p> <p style="background-color:blue">This is a gfg.</p> <button>background-color of p</button> </body> </html> Output: Before click on the button: After click on the button: Comment More infoAdvertise with us Next Article jQuery | Get and Set CSS Classes J jeetesh16 Follow Improve Article Tags : Web Technologies JQuery jQuery-HTML/CSS Similar Reads How to remove all CSS classes using jQuery ? In this article, we will remove all CSS classes for an element using jQuery. To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element. Syntax: $(selector).removeClass(class_name, function(index, 2 min read jQuery addClass() Method The addClass() method is an inbuilt method in jQuery that is used to add more properties to each selected element. It can also be used to change the property of the selected element. This method can be used in two different ways: 1) By adding a Class name directly: Here, the Class name can be used d 2 min read jQuery .class Selector The jQuery .class selector specifies the class for an element to be selected. It should not begin with a number. It gives styling to several HTML elements. Syntax: $(".class") Parameter: class: This parameter is required to specify the class of the elements to be selected.Example 1: In this example, 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 jQuery hasClass() Method The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exist or not. Syntax: $(selector).hasClass(className);Parameter: It accepts a "className" parameter which specifies the class name needed to search in the selected element. Return Value: It r 1 min read How to Add or Remove class in jQuery ? In this article, we are going to learn about the different methods to add and remove classes from an HTML element using jQuery. There are two ways in which we can achieve this in jQuery.Table of Content Using the addClass() and removeClass() methodsUsing the toggleClass() methodUsing the addClass() 3 min read How to fire event if CSS class is changed using jQuery ? 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 t 2 min read How CSS classes can be manipulated in HTML using jQuery ? In this article, we will learn how CSS classes can be manipulated in HTML using jQuery. The common manipulation of classes includes actions like adding a class or removing a class from the HTML tags. The following classes are used for the manipulations. addClass()removeClass()toggleClass() addClass( 2 min read How to Add and Remove multiple classes in jQuery ? Given an HTML element and the task is to add and remove multiple classes from it using JQuery. Approach: First select the element to which multiple classes will be added. Then use addClass() method to add multiple classes to the element and removeClass() method to remove multiple classes. Example 1: 2 min read Wildcard Selectors (*, ^ and $) in CSS for classes Wildcard selectors can be used with attribute selectors. The asterisk (*) matches any character, the caret (^) matches the start, and the dollar sign ($) matches the end of an attribute value. For example, .class* selects all elements with a class attribute containing "class" anywhere, .class^ selec 3 min read Like