classList in JavaScript: adding and removing CSS classes cleanly

classList offers five methods to work on the class attribute of an element: add, remove, toggle, contains and replace, never wiping the other classes.
3 min read
Believemy logo

Changing the look of an element from JavaScript almost never means writing colors and margins by hand. It means putting a class on or taking it off, and letting the stylesheet do the rest.

That separation rests on a quiet object, present on every element: classList.


Definition

classList is a view over the class attribute of an element, shaped as a list of words. It exposes methods that add or remove one class without touching the others, and it mirrors the real state of the element at any moment.

JAVASCRIPT
const card = document.querySelector(".card");

card.classList.add("open");
card.classList.remove("hidden");

console.log(card.classList.contains("open"));   // true
console.log(card.classList.length);


The five methods

MethodWhat it does
addAdds one or more classes, never duplicating
removeRemoves one or more classes, no error when absent
toggleAdds the class when missing, removes it otherwise
containsHands back a Boolean telling whether the class is there
replaceSwaps one class for another in a single call

Names go in separately: add("a", "b") adds two classes, while add("a b") throws, since a space is forbidden inside a class name.


toggle and its second argument

A second boolean argument forces the outcome: true adds, false removes. The method then mirrors a state directly, which saves a three-line if.

JAVASCRIPT
const panel = document.querySelector("#panel");
const isOpen = false;

panel.classList.toggle("visible", isOpen);   // removes "visible"
const present = panel.classList.toggle("visible");
console.log(present);   // true: the class was just added

With no second argument, the returned value tells whether the class is present after the call.


Why not className

className is the whole attribute string. Writing to it replaces everything, including classes placed by another part of the code or by the server.

JAVASCRIPT
// <div class="card shadow selected">
card.className = "open";        // the three classes disappear
card.classList.add("open");     // the three classes remain

Reading it stays handy at times, for instance to log the state of an element in the console. For any change, classList is the right answer.


Frequently asked questions

Question

Should presence be checked before adding?

No, add ignores a class already there and remove ignores a class that is absent. Both methods are no-ops in those cases, which makes any prior test pointless. contains is there to decide something else, not to guard the call.


Question

How do you get the list of classes?

The Spread (...) operator turns it into an Array, which opens up map and filter. The value property hands back the full string instead, identical to className, handy for a log line or a quick comparison.


Question

Is changing style directly a better idea?

Rarely. A value written into style becomes an inline style, which outranks the stylesheet and grows hard to undo. A class keeps the styling in the same place as the rest of the CSS. style stays justified for a computed value, such as a position in pixels.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.