DOM Element.classList Property
The classList property gives a live list of an element's CSS classes, with methods to add, remove, toggle and check classes.
Description
The read-only classList property of the Element interface returns a live DOMTokenList of the element's class names. It is the easiest way to change the classes of an element without editing the class attribute string by hand.
Use add() and remove() to change classes (both accept several names), toggle() to switch one on or off, contains() to check for one and replace() to swap one for another. toggle() also takes a second argument to force the class on or off.
Although the property itself is read-only, the list it returns is live: it updates when the class attribute changes, and changes made through it update the attribute. Toggling classes and letting CSS handle the styling is usually better than changing styles directly in JavaScript.
Syntax
element.classListValue
A live DOMTokenList of the element's classes, with methods such as add(), remove(), toggle() and contains().
Example
Browser Support
The following information will show you the current browser support for the Element.classList property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





