jQuery CSS Classes Manipulation

jQuery provides a set of methods for dynamically adding, removing, toggling, and checking CSS classes on HTML element

Methods for CSS Class Manipulation

  1. .addClass()
    • Adds one or more classes to the selected elements.

    Syntax:

    $(selector).addClass(className);
    

    Try It Now

    Example:

    $("#myDiv").addClass("highlight");
    

    Try It Now

     

  2. .removeClass()
    • Removes one or more classes from the selected elements.

    Syntax:

    $(selector).removeClass(className);
    

    Try It Now

    Example:

    $("#myDiv").removeClass("highlight");
    

    Try It Now

     

  3. .toggleClass()
    • Adds the class if it doesn’t exist; removes it if it does. This is useful for toggling between states.

    Syntax:

    $(selector).toggleClass(className);
    

    Try It Now

    Example:

    $("#myDiv").toggleClass("highlight");
    

    Try It Now

     

  4. .hasClass()
    • Checks whether an element has a specific class.

    Syntax:

    $(selector).hasClass(className);
    

    Try It Now

    Example:

    if ($("#myDiv").hasClass("highlight")) {
        alert("The element has the 'highlight' class.");
    }
    

    Try It Now

     

Examples

Adding and Removing Classes

<div id="myDiv">Hello World</div>
<button id="addClass">Add Class</button>
<button id="removeClass">Remove Class</button>

Try It Now

$("#addClass").click(function() {
    $("#myDiv").addClass("highlight");
});

$("#removeClass").click(function() {
    $("#myDiv").removeClass("highlight");
});

Try It Now

 

Toggling Classes

<div id="myDiv">Toggle Me</div>
<button id="toggleClass">Toggle Class</button>

Try It Now

$("#toggleClass").click(function() {
    $("#myDiv").toggleClass("highlight");
});

Try It Now

Checking for a Class

<div id="myDiv" class="highlight">Check Me</div>
<button id="checkClass">Check Class</button>

Try It Now

$("#checkClass").click(function() {
    if ($("#myDiv").hasClass("highlight")) {
        alert("The element has the 'highlight' class.");
    } else {
        alert("The element does not have the 'highlight' class.");
    }
});

Try It Now

Applying Multiple Classes

You can add or remove multiple classes at once by separating them with spaces.

$("#myDiv").addClass("class1 class2");
$("#myDiv").removeClass("class1 class2");

Try It Now

Using a Function in Class Methods

You can pass a function to .addClass(), .removeClass(), or .toggleClass() to dynamically decide the class name.

$("#myDiv").addClass(function(index, currentClass) {
    if (currentClass === "highlight") {
        return "highlight2";
    } else {
        return "highlight";
    }
});

Try It Now

Practical Use Case: Active Menu Item

<ul>
    <li class="menu">Home</li>
    <li class="menu">About</li>
    <li class="menu">Contact</li>
</ul>

Try It Now

$(".menu").click(function() {
    $(".menu").removeClass("active");
    $(this).addClass("active");
});

Try It Now

When a menu item is clicked, the active class is applied to the clicked item, and removed from the others.

Best Practices

  1. Avoid Inline Styling: Use CSS classes for styling and manipulate them dynamically with jQuery, rather than directly altering styles.
  2. Optimize Performance: For large DOM manipulations, batch your operations to avoid unnecessary reflows or repaints.
  3. Event Delegation: If adding classes to dynamically created elements, use event delegation with .on().

Summary

jQuery’s class manipulation methods—.addClass(), .removeClass(), .toggleClass(), and .hasClass()—offer powerful tools for dynamically modifying the appearance and behavior of elements.