Element.classList is a read-only property that returns a liveDOMTokenList of the element’s class attribute (MDN). Use it to add, remove, toggle, and check class names safely.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use el.classList. Start with this line.
JavaScript
el.classList
Call
What it does
el.classList
Primary signature.
Built into the browser. Read or assign el.classList — no import required.
Library declarations
JavaScript
el.classList
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
Live DOMTokenList
JavaScript Examples
Example 1 — Add and Remove (MDN)
Swap one class for another without rewriting the whole string.
add("foo"). The token is inserted into the live list.
remove("foo"). That token is dropped; other classes (if any) stay.
add again.className reflects the updated attribute string.
Example 2 — Toggle and Contains
Flip a class, then force it on or off with the second argument.
JavaScript
const el = document.getElementById("panel");
el.classList.toggle("open"); // flip
console.log(el.classList.contains("open")); // true or false
el.classList.toggle("open", true); // force on
el.classList.toggle("open", false); // force off
console.log(el.classList.contains("open")); // false
Multi-add.add("foo", "bar") inserts both tokens (MDN).
replace. Swaps foo for baz; bar stays.
Multi-remove. Clears both remaining tokens in one call.
length 0. An empty DOMTokenList means no classes are set.
Compatibility
Browser Support
Element.classList is Baseline Widely available (MDN: across browsers since October 2017).
✓ Baseline · Widely available
Element.classList
Supported in modern Chromium, Firefox, Safari, and Edge. Prefer classList over manual className string edits in new code.
98%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
Element.classListExcellent
Bottom line: Safe for modern web apps. Use add/remove/toggle/replace for clear, bug-resistant class updates.
Frequently Asked Questions
It returns a live DOMTokenList of the element’s class attribute tokens. Use it to add, remove, toggle, check, or replace CSS class names without string parsing.
No. MDN marks Element.classList as Baseline Widely available (since October 2017). It is not Deprecated, Experimental, or Non-standard.
The property itself is read-only (you cannot replace the DOMTokenList object), but you can mutate it with add/remove/toggle/replace, or assign a string which updates classList.value (MDN).
className is the whole class attribute as one space-separated string. classList is a token list API for safer per-class updates.
toggle(token) adds the class if missing and removes it if present. With a second force boolean, it adds when true and removes when false.
Yes. classList.add("a", "b") and classList.remove("a", "b") accept multiple tokens. You can also spread an array: add(...cls).