jQuery toggleClass() Method — Toggle CSS Classes

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

.toggleClass(className) adds a CSS class when it is missing and removes it when it is present (since jQuery 1.0). Optional state forces add/remove (1.3+); arrays work since 3.3.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("div").toggleClass("highlight")           // flip
$("div").toggleClass("highlight", true)     // force on
$("div").toggleClass("a b")                 // each name flips
$("div").toggleClass(["a", "b"])            // array (3.3+)

// true  → like .addClass()
// false → like .removeClass()
CallWhat it does
toggleClass()Learn jQuery .toggleClass(): flip CSS classes on or off, boolean state, arrays and callbacks, vs addClass and removeClass, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s) with a jQuery selector.
Step 2Call .toggleClass("name") — or a space-separated list / array.
Step 3Optional: pass true/false as the second argument to force on/off.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Toggle a Class on Click

Official style: flip highlight each time a paragraph is clicked.

jQuery
$("p").on("click", function () {
  $(this).toggleClass("highlight");
  console.log(this.className);
});
// first click  → "… highlight"
// second click → highlight removed
Try It Yourself

How It Works

  1. Click handler. Each paragraph toggles itself with $(this).
  2. Missing class. First click adds highlight.
  3. Present class. Next click removes it.

Example 2 — Force With a Boolean State

Official pattern: add highlight only every third click.

jQuery
var count = 0;
$("p").on("click", function () {
  count++;
  $(this).toggleClass("highlight", count % 3 === 0);
  console.log(count, this.className);
});
// clicks 1–2 → highlight off
// click 3    → highlight on
Try It Yourself

How It Works

  1. Count clicks in a variable outside the handler.
  2. Boolean. count % 3 === 0 is a real true/false.
  3. Force. true adds; false removes — no flip surprise.

Example 3 — Toggle Several Classes

Each listed class flips independently.

jQuery
var $box = $("<div class='a'></div>");
$box.toggleClass("a b");
console.log($box.attr("class"));  // "b"

$box.toggleClass(["a", "b"]);
console.log($box.attr("class"));  // "a"
Try It Yourself

How It Works

  1. Start. Element has class a only.
  2. String. "a b" removes a and adds b.
  3. Array. ["a", "b"] flips both again → back to a.

Frequently Asked Questions

.toggleClass(className) adds a class when it is missing and removes it when it is present — flipping the class on or off in one call. It updates the class attribute so stylesheet rules apply or stop applying.
.addClass() always adds; .removeClass() always removes. .toggleClass() inspects each element and flips the class. Use it for click-to-highlight, open/close UI, and theme switches without tracking state yourself.
Since jQuery 1.3, .toggleClass(className, state) forces the result: true adds (like .addClass()), false removes (like .removeClass()). Handy when you already know the desired state, e.g. count % 3 === 0.
Yes. Pass a space-separated string or, since jQuery 3.3, an array. Each listed class is toggled independently on every matched element.
Since jQuery 1.4, pass a function that returns the class name(s) to toggle. jQuery calls it once per element with index, current className, and state. Since 3.3 it may return an array.
No. Calling .toggleClass() with only an optional boolean (toggling all classes) was deprecated in jQuery 3.0 and removed in 4.0. Always pass the class name(s) you mean to flip.

Next: css() Method — Get and Set Styles

Learn how to get and set inline CSS properties with jQuery.

css() Method — Get and Set Styles tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful