Element: classList property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

The read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.

Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.

Value

A DOMTokenList object representing the contents of the element's class attribute. If the class attribute is not set or empty, it returns an empty DOMTokenList, i.e., a DOMTokenList with the length property equal to 0.

Although the classList property itself is read-only in the sense that you can't replace the DOMTokenList object, you can still assign to the classList property directly, which is equivalent to assigning to its value property. You can also modify the DOMTokenList object using the add(), remove(), replace(), and toggle() methods.

Examples

js
const div = document.createElement("div");
div.classList = "foo"; // forwarded to classList.value

// our starting state: <div class="foo"></div>
console.log(div.outerHTML);

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("another-class");

// <div class="another-class"></div>
console.log(div.outerHTML);

// if visible is set remove it, otherwise add it
div.classList.toggle("visible");

// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10);

// false
console.log(div.classList.contains("foo"));

// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");

// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);

// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");

Specifications

Specification
DOM
# ref-for-dom-element-classlist①

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
classList
Chrome – Partial support
Chrome 8 – 21 (Release date: 2010-12-02)
footnote Partial support
footnote Not supported for SVG elements.
Chrome – Full support
Chrome 22 (Release date: 2012-09-25)
footnote Full support
Edge – Partial support
Edge 12 – 15 (Release date: 2015-07-29)
footnote Partial support
footnote Not supported for SVG elements.
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 3.6 (Release date: 2010-01-21)
footnote Full support
Opera – Full support
Opera 11.5 (Release date: 2011-06-28)
footnote Full support
Safari – Partial support
Safari 6 – 6 (Release date: 2012-07-25)
footnote Partial support
footnote Not supported for SVG elements.
Safari – Full support
Safari 7 (Release date: 2013-10-22)
footnote Full support
Chrome Android – Partial support
Chrome Android 18 – 18 (Release date: 2012-06-27)
footnote Partial support
footnote Not supported for SVG elements.
Chrome Android – Full support
Chrome Android 25 (Release date: 2013-02-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 11.5 (Release date: 2011-10-12)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 6 – 6 (Release date: 2012-09-10)
footnote Partial support
footnote Not supported for SVG elements.
Safari on iOS – Full support
Safari on iOS 7 (Release date: 2013-09-18)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 1 – 1 (Release date: 2013-04-27)
footnote Partial support
footnote Not supported for SVG elements.
Samsung Internet – Full support
Samsung Internet 1.5 (Release date: 2013-09-25)
footnote Full support
WebView Android – Partial support
WebView Android 3 – 4 (Release date: 2011-02-22)
footnote Partial support
footnote Not supported for SVG elements.
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Partial support
WebView on iOS 6 – 6 (Release date: 2012-09-10)
footnote Partial support
footnote Not supported for SVG elements.
WebView on iOS – Full support
WebView on iOS 7 (Release date: 2013-09-18)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

See also