How CSS classes can be manipulated in HTML using jQuery ? Last Updated : 05 Jun, 2023 Comments Improve Suggest changes Like Article Like Report 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() method: The purpose of the addClass() function is to add one or more classes to the specified element or tag. Syntax: $('selector expression').addClass('class name'); Example: In this example, we will use addClass() method. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <style> p{ border:1px solid black; text-align: center; padding:2rem; margin: 2rem; } .bgred{ background-color: red; } button{ margin:0 10rem; } </style> </head> <body> <center> <h2 style="color:green">GeeksforGeeks</h2> <p class="">Hi this is paragraph</p> <button id="btn">Change background</button> </center> <script> $('button').click(()=>{ $('p').addClass('bgred') }) </script> </body> </html> Output: removeClass() method: We use the removeClass() function to remove one or more or all classes from the specified element or tag. Syntax: $('selector expression').removeClass('class name'); Example: In this example, we will use the removeClass() method. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <style> p{ border:1px solid black; text-align: center; padding:2rem; margin: 2rem; } .bgred{ background-color: red; } button{ margin:0 10rem; } </style> </head> <body> <center> <h2 style="color:green">GeeksforGeeks</h2> <p class="bgred">Hi this is paragraph</p> <button id="btn">Change background</button> </center> <script> $('button').click(()=>{ $('p').removeClass('bgred'); }) </script> </body> </html> Output: toggleClass() method: We use the toggleClass() function to simultaneously add or remove the class to the specified element or tag. Syntax: $('selector expression').addClass('class name'); Example: In this example, we will use the toggleClass() method. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <style> p{ border:1px solid black; text-align: center; padding:2rem; margin: 2rem; } .bgred{ background-color: red; } button{ margin:0 10rem; } </style> </head> <body> <center> <h2 style="color:green">GeeksforGeeks</h2> <p class="bgred">Hi, this is paragraph</p> <button id="btn">Change background</button> </center> <script> $('button').click(()=>{ $('p').toggleClass('bgred'); }) </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How CSS classes can be manipulated in HTML using jQuery ? M mahalenachiket77 Follow Improve Article Tags : Web Technologies JQuery jQuery-Questions 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 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 How to create a CSS rule or class at runtime using jQuery ? It's a common practice to write a CSS file for our HTML. Those are called static CSS. When we want to create our Cascading Style Sheet rule at runtime, we need jQuery. The jQuery enables us to apply styles to our HTML dynamically. The CSS rules once written can be stored in a variable and used multi 2 min read How to modify CSS class using jQuery library ? In this article, we will discuss how to modify the css class using jQuery. This is one of the major applications of jQuery. We generally use it when we have to add a dynamic style of the particular element of the HTML. For example, changing the color of particular class content dynamically.Approach: 5 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 Select and Manipulate CSS pseudo-elements using JavaScript / jQuery ? In this article, we will learn how to select and manipulate CSS pseudo-elements using JavaScript (or jQuery). CSS pseudo-elements allow developers to style and enhance specific parts of an HTML document with the help of selectors like::before and::after, which provide the flexibility to add decorati 2 min read jQuery | Get and Set CSS Classes 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 retu 3 min read How to add a class on click of anchor tag using jQuery ? In this article, we will see how to add a class on click of an anchor tag using jQuery. To add a class on click of the anchor tag, we use the addClass() method. The addClass() method is used to add more properties to each selected element. It can also be used to change the property of the selected e 2 min read How to Toggle Between Two Classes in jQuery ? In this article, we will see how to toggle between two classes in jQuery. To make the classes toggle, we will use toggleClass() method. The toggleClass() method is used to toggle or switch the class with the selected class element. Syntax: $(selector).toggleClass(class1 class2) Example 1: In this ex 2 min read How to apply CSS style using jQuery ? In this article, we will explore how we can apply CSS styles to HTML elements. It is recommended that you have some basic knowledge of HTML, CSS, JavaScript, and jQuery before beginning this article. It is possible to change the CSS property of an element by using a simple JavaScript API, but we try 2 min read Like