web api interface
DOMTokenList
The DOMTokenList interface represents a set of space-separated tokens. Such a set is returned by classList or relList, and many others.
A DOMTokenList is indexed beginning with 0 as with JavaScript Array objects. DOMTokenList is always case-sensitive.
Instance properties
lengthRead only- : An
integerrepresenting the number of objects stored in the object.
- : An
value- : A stringifier property that returns the value of the list as a string.
Instance methods
item()- : Returns the item in the list by its index, or
nullif the index is greater than or equal to the list’slength.
- : Returns the item in the list by its index, or
contains()- : Returns
trueif the list contains the given token, otherwisefalse.
- : Returns
add()- : Adds the specified tokens to the list.
remove()- : Removes the specified tokens from the list.
replace()- : Replaces the token with another one.
supports()- : Returns
trueif the given token is in the associated attribute’s supported tokens.
- : Returns
toggle()- : Removes the token from the list if it exists, or adds it to the list if it doesn’t. Returns a boolean indicating whether the token is in the list after the operation.
entries()- : Returns an
iterator, allowing you to go through all key/value pairs contained in this object.
- : Returns an
forEach()- : Executes a provided callback function once for each
DOMTokenListelement.
- : Executes a provided callback function once for each
keys()- : Returns an
iterator, allowing you to go through all keys of the key/value pairs contained in this object.
- : Returns an
toString()- : Returns the
value, the space-separated values of the list as a string.
- : Returns the
values()- : Returns an
iterator, allowing you to go through all values of the key/value pairs contained in this object.
- : Returns an
Examples
In the following simple example, we retrieve the list of classes set on a <p> element as a DOMTokenList using classList, add a class using add(), and then update the textContent of the <p> to equal the DOMTokenList.
First, the HTML:
<p class="a b c"></p>
Now the JavaScript:
let para = document.querySelector("p");
let classes = para.classList;
para.classList.add("d");
para.textContent = `paragraph classList is "${classes}"`;
The output looks like this:
Interactive exampleOpen the canonical MDN source to run this embedded demo.
Trimming of whitespace and removal of duplicates
Methods that modify the DOMTokenList (such as add()) automatically trim any excess Whitespace and remove duplicate values from the list. For example:
<span class=" d d e f"></span>
let span = document.querySelector("span");
let classes = span.classList;
span.classList.add("x");
span.textContent = `span classList is "${classes}"`;
The output looks like this:
Interactive exampleOpen the canonical MDN source to run this embedded demo.
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.