.toggleClass()

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.

.toggleClass(className)🡢 jQuery

className StringOne or more classes (separated by spaces) to be toggled for each element in the matched set.

.toggleClass(className, state)🡢 jQuery

className StringOne or more classes (separated by spaces) to be toggled for each element in the matched set.
state BooleanA boolean (not just truthy/falsy) value to determine whether the class should be added or removed.

.toggleClass(classNames)🡢 jQuery

classNames ArrayAn array of classes to be toggled for each element in the matched set.

.toggleClass(classNames, state)🡢 jQuery

classNames ArrayAn array of classes to be toggled for each element in the matched set.
state BooleanA boolean (not just truthy/falsy) value to determine whether the class should be added or removed.

.toggleClass(function(indexInteger, classNameString, stateBoolean), state)🡢 String

function(indexInteger, classNameString, stateBoolean) FunctionA function returning one or more space-separated class names to be toggled in the class attribute of each element in the matched set. Receives the index position of the element in the set, the old class value, and the state as arguments.
state BooleanA boolean value to determine whether the class should be added or removed.

.toggleClass(function(indexInteger, classNameString, stateBoolean), state)🡢 String, Array

function(indexInteger, classNameString, stateBoolean) FunctionA function returning one or more space-separated class names or an array of class names to be toggled in the class attribute of each element in the matched set. Receives the index position of the element in the set, the old class value, and the state as arguments.
state BooleanA boolean value to determine whether the class should be added or removed.

This method takes one or more classes as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply .toggleClass() to a simple <div>:

<div class="tumble">Some text.</div>

The first time we apply $( "div.tumble" ).toggleClass( "bounce" ), we get the following:

<div class="tumble bounce">Some text.</div>

The second time we apply $( "div.tumble" ).toggleClass( "bounce" ), the <div> class is returned to the single tumble value:

<div class="tumble">Some text.</div>

Applying .toggleClass( "bounce spin" ) to the same <div> alternates between <div class="tumble bounce spin"> and <div class="tumble">.

The second version of .toggleClass() uses the second parameter for determining whether the class should be added or removed. If this parameter's value is true, then the class is added; if false, the class is removed. In essence, the statement:

$("#foo").toggleClass(className, addOrRemove);

is equivalent to:

if (addOrRemove) {
  $("#foo").addClass(className);
} else {
  $("#foo").removeClass(className);
}

As of jQuery 1.4, if no arguments are passed to .toggleClass(), all classes on the element the first time .toggleClass() is called will be toggled. Also as of jQuery 1.4, the class name to be toggled can be determined by passing in a function.

$("div.foo").toggleClass(function () {
  if ($(this).parent().is(".bar")) {
    return "happy";
  } else {
    return "sad";
  }
});

This example will toggle the happy class for <div class="foo"> elements if their parent element has a class of bar; otherwise, it will toggle the sad class.

Toggle the class 'highlight' when a paragraph is clicked.

JS
<p class="blue">Click to toggle</p>
<p class="blue highlight">highlight</p>
<p class="blue">on these</p>
<p class="blue">paragraphs</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
  cursor: pointer;
}
.blue {
  color: blue;
}
.highlight {
  background: yellow;
}
HTML
$("p").click(function () {
  $(this).toggleClass("highlight");
});
DEMO

Add the "highlight" class to the clicked paragraph on every third click of that paragraph, remove it every first and second click.

JS
<p class="blue">Click to toggle (<span>clicks: 0</span>)</p>
<p class="blue highlight">highlight (<span>clicks: 0</span>)</p>
<p class="blue">on these (<span>clicks: 0</span>)</p>
<p class="blue">paragraphs (<span>clicks: 0</span>)</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
  cursor: pointer;
}
.blue {
  color: blue;
}
.highlight {
  background: red;
}
HTML
var count = 0;
$("p").each(function () {
  var $thisParagraph = $(this);
  var count = 0;
  $thisParagraph.click(function () {
    count++;
    $thisParagraph.find("span").text("clicks: " + count);
    $thisParagraph.toggleClass("highlight", count % 3 === 0);
  });
});
DEMO

Toggle the class name(s) indicated on the buttons for each div.

JS
<div class="buttons">
  <button>toggle</button>
  <button class="a">toggle a</button>
  <button class="a b">toggle a b</button>
  <button class="a b c">toggle a b c</button>
  <a href="#">reset</a>
</div>
<div class="wrap">
  <div></div>
  <div class="b"></div>
  <div class="a b"></div>
  <div class="a c"></div>
</div>
CSS
.wrap > div {
  float: left;
  width: 100px;
  margin: 1em 1em 0 0;
  padding-left: 3px;
  border: 1px solid #abc;
}
div.a {
  background-color: aqua;
}
div.b {
  background-color: burlywood;
}
div.c {
  background-color: cornsilk;
}
HTML
var cls = ["", "a", "a b", "a b c"];
var divs = $("div.wrap").children();
var appendClass = function () {
  divs.append(function () {
    return "<div>" + (this.className || "none") + "</div>";
  });
};

appendClass();

$("button").on("click", function () {
  var tc = this.className || undefined;
  divs.toggleClass(tc);
  appendClass();
});

$("a").on("click", function (event) {
  event.preventDefault();
  divs.empty().each(function (i) {
    this.className = cls[i];
  });
  appendClass();
});
DEMO

.toggleClass(state)🡢 jQuery

state BooleanA boolean value to determine whether the class should be added or removed.
This signature (only!) is deprecated as of jQuery 3.0.

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)