plainJS

Functions / Attributes

Adding, removing, and testing for classes

jQuery $.addClass(), $.removeClass(), $.hasClass(), $.toggleClass()

In short

Use element.classList. It has add(), remove(), toggle(), replace() and contains(), and it has worked in every browser since October 20171. The helper functions this page carried from 2015 are no longer needed.

  • el.classList.add("a", "b") adds one or more classes; remove() takes the same arguments1
  • el.classList.toggle("open") flips a class; a second argument forces it on or off1
  • el.classList.contains("open") is the test; replace("a", "b") swaps one class for another1

The answer

htmlthe page
<nav id="menu" class="menu">Menu</nav>
<button id="open">Open</button>
js
const menu = document.querySelector("#menu");

menu.classList.add("is-open");            // jQuery: $(menu).addClass("is-open")
menu.classList.remove("is-open");         // $(menu).removeClass("is-open")
menu.classList.toggle("is-open");         // $(menu).toggleClass("is-open")
menu.classList.contains("is-open");       // $(menu).hasClass("is-open") -> true

menu.classList.add("wide", "dark");       // several at once
menu.classList.replace("dark", "light");  // swap one class for another

classList is a live list of the element’s classes, read from the class attribute and written back to it as you change it1. Reading el.className still gives you the whole attribute as one string, which is useful for debugging and for nothing else.

Advertisement

In jQuery

jsjQuery
$("#menu").addClass("is-open wide");
$("#menu").removeClass("is-open");
$("#menu").toggleClass("is-open");
$("#menu").hasClass("is-open");

jQuery’s .addClass() adds the class or classes to every element in the matched set and takes a space-separated string, or an array since jQuery 3.32.

Browser support classList is Baseline widely available: MDN records it as available across browsers since October 20171. Nothing on this page needs a fallback for a browser released in the last eight years.

Differs from jQuery classList belongs to one element. jQuery methods act on every element in the set at once; in plain JavaScript you loop (next section). Several classes are several arguments, add("a", "b"), not one space-separated string: add("a b") throws, because a class name cannot contain a space1. toggle() takes a second argument that forces the class on or off1; jQuery’s .toggleClass() has the same second argument.

Several elements at once

querySelectorAll() returns a static list; forEach walks it.

js
document.querySelectorAll(".card").forEach((card) => {
  card.classList.add("is-visible");
});

// toggle depending on a condition, the way jQuery's second argument does
document.querySelectorAll(".card").forEach((card, i) => {
  card.classList.toggle("is-featured", i < 3);
});

Toggling from an event

The example above has a button. Wire it up. toggle() returns true when the class is in the list after the call and false when it is not, so the handler knows the new state without a second lookup3.

js
document.querySelector("#open").addEventListener("click", () => {
  const isOpen = document.querySelector("#menu").classList.toggle("is-open");
  console.log(isOpen ? "opened" : "closed");
});
Legacy browsers: Internet Explorer 9 and older

The 2015 version of this page shipped helper functions that fell back to a regular expression on className when classList was missing. They still work, and they are kept here for anyone maintaining a page that must run in Internet Explorer 9 or older. Do not start new work with them.

js
function hasClass(el, className) {
  return el.classList ? el.classList.contains(className) : new RegExp("\\b" + className + "\\b").test(el.className);
}
function addClass(el, className) {
  if (el.classList) el.classList.add(className);
  else if (!hasClass(el, className)) el.className += " " + className;
}
function removeClass(el, className) {
  if (el.classList) el.classList.remove(className);
  else el.className = el.className.replace(new RegExp("\\b" + className + "\\b", "g"), "");
}
addClass(document.querySelector("#menu"), "legacy-tested");

Sources

  1. Element: classList property, MDN (accessed 2026-10-29)
  2. .addClass(), jQuery API documentation (accessed 2026-10-29)
  3. DOMTokenList: toggle() method, MDN (accessed 2026-10-29)

Last reviewed 2026-10-30. Tested in Chromium 148, 2026-10-29 by the snippet harness.