• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Toggle Class of HTML Element

Using jQuery to Toggle Class of HTML Element

December 2, 2021 Leave a Comment

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:

<div id="div" class="example-class">
  <p class="p">This is a paragraph.</p>
</div>

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:

<div id="div">
  <p class="p">This is a paragraph.</p>
</div>

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

<div id="div" class="example-class">
  <p class="p">This is a paragraph.</p>
</div>

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.

<style>.p-underline { text-decoration:underline; } </style>
<div id="div1">
  <p class="p">This is the paragraph where we will toggle a class.</p>
  <p id="click-me" class="button-div">Toggle class</p>
</div>

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:

<style>.p-underline { text-decoration:underline; } </style>
<div id="div1">
  <p class="p">This is the paragraph where we will toggle the class.</p>
  <p id="click-me" class="button-div">Toggle class</p>
</div>

<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:

<style>.class-1 { text-decoration:underline; } .class-2 { background-color: yellow; }</style>
<div id="div">
  <p id="click-me">Toggle Classes</p>
</div>

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:

<style>.class-1 { text-decoration:underline; } .class-2 { background-color: yellow; }</style>
<div id="div">
  <p id="click-me">Toggle Multiple Classes</p>
</div>

<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.

Other Articles You'll Also Like:

  • 1.  Examples of Text Animations Using jQuery
  • 2.  Using jQuery to Remove the Id of a Div
  • 3.  jQuery Display None
  • 4.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 5.  jQuery get first child
  • 6.  Using jQuery to Get the Bottom Position of Element
  • 7.  Using jQuery to Get the Current URL
  • 8.  Using jQuery to Add Required Attribute to Input Field
  • 9.  Using jQuery to Change the Text Color of a Paragraph
  • 10.  Using jQuery to Select Multiple Ids

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy