Last Updated: Feb 3, 2025
Easy

How to Add or Remove class in jQuery

Table of contents
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

How to Add or Remove Class in jQuery is a technique that allows developers to manipulate the classes of HTML elements dynamically, enhancing the functionality and interactivity of a webpage. By using jQuery’s .addClass() and .removeClass() methods, you can easily apply or remove classes from elements, enabling features like animations, styling changes, and event-driven interactions without requiring complex JavaScript.

How to Add or Remove class in jQuery

In this article, you will learn about the syntax of jQuery's class manipulation methods, how to use them effectively, and practical examples to improve your web development skills.

Definition and Usage  

jQuery makes it easy to work with CSS classes on HTML elements. Classes are used to style elements, & jQuery provides methods to add, remove, or toggle these classes dynamically. This is especially useful when you want to change the look or behavior of an element based on user interactions, like clicking a button or hovering over an image.  

Let’s understand this in detail:  

1. Adding a Class: You can add a class to an element using the `.addClass()` method. This method takes the name of the class as a parameter & applies it to the selected element.  
 

2. Removing a Class: To remove a class, you use the `.removeClass()` method. This removes the specified class from the element.  
 

3. Toggling a Class: The `.toggleClass()` method is used to add a class if it’s not already present or remove it if it is. This is handy for creating toggle effects. 
 

For example: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery Class Manipulation</title>
    <style>
        .highlight {
            background-color: yellow;
            font-weight: bold;
        }
        .border {
            border: 2px solid red;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <p id="text">This is a sample text.</p>
    <button id="addClassBtn">Add Class</button>
    <button id="removeClassBtn">Remove Class</button>
    <button id="toggleClassBtn">Toggle Class</button>
    <script>
        $(document).ready(function() {
            // Add class on button click
            $("addClassBtn").click(function() {
                $("text").addClass("highlight border");
            });


            // Remove class on button click
            $("removeClassBtn").click(function() {
                $("text").removeClass("highlight");
            });
           // Toggle class on button click
            $("toggleClassBtn").click(function() {
                $("text").toggleClass("highlight");
            });
        });
    </script>
</body>
</html>


In this Code:  

  • We have a paragraph (`<p>`) with the ID `text` & three buttons.  
     
  • The first button adds two classes (`highlight` & `border`) to the paragraph when clicked.  
     
  • The second button removes the `highlight` class.  
     
  • The third button toggles the `highlight` class, meaning it adds the class if it’s not present & removes it if it is.  


This example shows how jQuery simplifies class manipulation. You can use these methods to create dynamic effects on your webpage without writing complex JavaScript.  

Using the addClass() and removeClass() Methods

The addClass() and removeClass() methods are used to add and remove CSS classes, respectively. These methods help in modifying the style of HTML elements dynamically without modifying the HTML or CSS files directly.

Syntax

addClass() Method

$(selector).addClass(className);

 

  • selector: Specifies the HTML element to which the class will be added.
     
  • className: The CSS class that will be added to the selected element.
     

removeClass() Method

$(selector).removeClass(className);

 

  • selector: Specifies the HTML element from which the class will be removed.
     
  • className: The CSS class that will be removed from the selected element.

Example

Using addClass() and removeClass()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery addClass and removeClass Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .highlight {
            background-color: yellow;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <p id="text">This is a sample text.</p>
    <button id="add">Add Class</button>
    <button id="remove">Remove Class</button>

    <script>
        $(document).ready(function() {
            $("#add").click(function() {
                $("#text").addClass("highlight");
            });
            
            $("#remove").click(function() {
                $("#text").removeClass("highlight");
            });
        });
    </script>
</body>
</html>

 

Output Explanation

  • Clicking the "Add Class" button adds the highlight class to the paragraph, changing its background color to yellow and making the text bold.
  • Clicking the "Remove Class" button removes the highlight class, restoring the paragraph to its default style.

Using the toggleClass() Method

The toggleClass() method is a combination of addClass() and removeClass(). It adds the class if it is not present and removes it if it is already applied.

Syntax

$(selector).toggleClass(className);

 

  • selector: Specifies the HTML element to modify.
     
  • className: The CSS class to be toggled.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery toggleClass Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .highlight {
            background-color: lightblue;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <p id="text">Click the button to toggle class.</p>
    <button id="toggle">Toggle Class</button>

   <script>
        $(document).ready(function() {
            $("#toggle").click(function() {
                $("#text").toggleClass("highlight");
            });
        });
    </script>
</body>
</html>

 

Output Explanation

  • Clicking the "Toggle Class" button adds the highlight class if it is not present, applying the light blue background and bold text.
     
  • Clicking the button again removes the highlight class, restoring the default style.

jQuery CSS Classes Manipulation  

jQuery provides a simple way to manipulate CSS classes, making it easier to control the styling of elements on your webpage. Let’s take a look at the three main methods for class manipulation: `.addClass()`, `.removeClass()`, & `.toggleClass()`.  

1. Adding a Class with `.addClass()`  

The `.addClass()` method is used to add one or more classes to an element. You can add multiple classes by separating them with a space.  

Syntax

$(selector).addClass(className);

Example  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
        .large-text {
            font-size: 24px;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <p id="text">This is a sample text.</p>
    <button id="addClassBtn">Add Classes</button>


    <script>
        $(document).ready(function() {
            $("addClassBtn").click(function() {
                $("text").addClass("highlight large-text");
            });
        });
    </script>
</body>
</html>


Explanation:  

  • When the button is clicked, the `highlight` & `large-text` classes are added to the paragraph.  
     
  • The text will now have a yellow background & a larger font size.  

2. Removing a Class with `.removeClass()`  

The `.removeClass()` method removes one or more classes from an element. If no class name is specified, it removes all classes.  

Syntax

$(selector).removeClass(className);

Example 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remove Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <p id="text" class="highlight">This is a sample text.</p>
    <button id="removeClassBtn">Remove Class</button>
    <script>
        $(document).ready(function() {
            $("removeClassBtn").click(function() {
                $("text").removeClass("highlight");
            });
        });
    </script>
</body>
</html>


Explanation:  

  • The paragraph initially has the `highlight` class, which gives it a yellow background. 
     
  • When the button is clicked, the `highlight` class is removed, & the yellow background disappears.  

3. Toggling a Class with `.toggleClass()`  

The `.toggleClass()` method adds a class if it’s not present & removes it if it is. This is useful for creating toggle effects.  

Syntax:  

$(selector).toggleClass(className);


Example:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Toggle Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <p id="text">This is a sample text.</p>
    <button id="toggleClassBtn">Toggle Class</button>
    <script>
        $(document).ready(function() {
            $("toggleClassBtn").click(function() {
                $("text").toggleClass("highlight");
            });
        });
    </script>
</body>
</html>


Explanation:  

  • Each time the button is clicked, the `highlight` class is toggled on or off.  
     
  • If the paragraph has the `highlight` class, it will be removed. If it doesn’t have the class, it will be added.  

These methods are powerful tools for dynamically changing the appearance of elements on your webpage. They are easy to use & can be combined to create complex interactions.  

Frequently Asked Questions

What is the difference between addClass() and toggleClass()?

addClass() only adds a class, whereas toggleClass() alternates between adding and removing the class based on its current state.

Can we add multiple classes using addClass()?

Yes, you can add multiple classes by separating them with spaces, like this:

$("#text").addClass("class1 class2 class3");

What happens if I call removeClass() on an element that doesn’t have the class?

Nothing will happen; removeClass() only removes the class if it is present, otherwise, it does nothing.

Conclusion

In this article, we will learn how to add or remove classes in jQuery. jQuery provides easy-to-use methods for manipulating CSS classes, allowing developers to enhance user interactivity and control the styling of elements. The addClass() method is used to add a class, while the removeClass() method removes a class from an element.