To toggle a class using jQuery, the simplest way is to use the jQuery toggleClass method.

$("#div").toggleClass("example-class");

Let’s say I have the following HTML:

This is a paragraph.

If we want to toggle the class “example-class” to #div, we can use the jQuery toggleClass() method to do this with the following Javascript code:

$("#div").toggleClass("example-class");

If our div already has the class “example-class”, it will be removed when we use the toggleClass() method.

In this case, the resulting HTML would be as follows:

This is a paragraph.

If we use the toggleClass() method again, we will add the class “example-class” back to the div.

This is a paragraph.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div").toggleClass("example-class");

Toggle Class on Click Using jQuery

We can toggle a class to a HTML element using jQuery very easily by combining the toggleClass() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to toggle the class on the paragraph. The class will underline and remove the underline from the paragraph element.


This is the paragraph where we will toggle a class.

Toggle class

We can utilize both the jQuery click() method and jQuery toggleClass() method to add a class to the paragraph and make the contents underlined.

Below is the Javascript code which will allow the user to be able to add a class to the paragraph:

$("#click-me").click(function(){
    $(".p").toggleClass("p-underline");
}); 

The final code and output for this example of how to toggle a class using jQuery and Javascript is below:

Code Output:

This is the paragraph where we will toggle the class.

Toggle class

Full Code:


This is the paragraph where we will toggle the class.

Toggle class

<script> $("#click-me").click(function(){ $(".p").toggleClass("p-underline"); }); </script>

Using jQuery to Toggle Multiple Classes on HTML Element

We can use the jQuery addClass() method to toggle multiple classes on an HTML element very easily.

The key to toggling multiple classes to our HTML elements is putting a space in between the classes we want to toggle in the call to toggleClass()

For example, if we want to toggle classes “class-1” and “class-2” on a specific div, we can do so with the following Javascript code:

$("#div").toggleClass("class-1 class-2");

Let’s say we have the following HTML:


Toggle Classes

If we want to toggle the classes “class-1” and “class-2” on this div after a click, we just need to do the following in our Javascript code:

$("#click-me").click(function(){
    $("#div").toggleClass("class-1 class-2");
}); 

The result will be that the div will have a changed background color and will have all of it’s text underlined.

The final code and output for this example of how to toggle multiple classes using jQuery and Javascript is below:

Code Output:

Toggle Multiple Classes

Full Code:


Toggle Multiple Classes

<script> $("#click-me").click(function(){ $("#div").toggleClass("class-1 class-2"); // this will toggle multiple classes }); </script>

Hopefully this article has been useful for you to understand how to use jQuery to toggle the class of an HTML Element.

Categorized in:

jQuery,

Last Update: March 4, 2024