DOMTokenList
{{APIRef("DOM")}}
The DOMTokenList interface represents a set of space-separated tokens. Such a set is returned by {{domxref("Element.classList")}} or {{domxref("HTMLLinkElement.relList")}} , and many others.
A DOMTokenList is indexed beginning with 0 as with JavaScript {{jsxref("Array")}} objects. DOMTokenList is always case-sensitive.
Instance properties
{{domxref("DOMTokenList.length")}}{{ReadOnlyInline}}- : An
integerrepresenting the number of objects stored in the object.
- : An
{{domxref("DOMTokenList.value")}}- : A
{{Glossary("stringifier")}}property that returns the value of the list as a string.
- : A
Instance methods
{{domxref("DOMTokenList.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
{{domxref("DOMTokenList.contains()")}}- : Returns
trueif the list contains the given token, otherwisefalse.
- : Returns
{{domxref("DOMTokenList.add()")}}- : Adds the specified tokens to the list.
{{domxref("DOMTokenList.remove()")}}- : Removes the specified tokens from the list.
{{domxref("DOMTokenList.replace()")}}- : Replaces the token with another one.
{{domxref("DOMTokenList.supports()")}}- : Returns
trueif the given token is in the associated attribute’s supported tokens.
- : Returns
{{domxref("DOMTokenList.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.
{{domxref("DOMTokenList.entries()")}}- : Returns an
{{jsxref("Iteration_protocols", "iterator", "", 1)}}, allowing you to go through all key/value pairs contained in this object.
- : Returns an
{{domxref("DOMTokenList.forEach()")}}- : Executes a provided callback function once for each
DOMTokenListelement.
- : Executes a provided callback function once for each
{{domxref("DOMTokenList.keys()")}}- : Returns an
{{jsxref("Iteration_protocols", "iterator", "", 1)}}, allowing you to go through all keys of the key/value pairs contained in this object.
- : Returns an
{{domxref("DOMTokenList.toString()")}}- : Returns the
{{domxref("DOMTokenList.value")}}, the space-separated values of the list as a string.
- : Returns the
{{domxref("DOMTokenList.values()")}}- : Returns an
{{jsxref("Iteration_protocols", "iterator", "", 1)}}, 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 {{htmlelement("p")}} element as a DOMTokenList using {{domxref("Element.classList")}} , add a class using {{domxref("DOMTokenList.add()")}} , and then update the {{domxref("Node.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:
{{ EmbedLiveSample('Examples', '100%', 60) }}
Trimming of whitespace and removal of duplicates
Methods that modify the DOMTokenList (such as {{domxref("DOMTokenList.add()")}} ) automatically trim any excess {{Glossary("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:
{{ EmbedLiveSample('Trimming_of_whitespace_and_removal_of_duplicates', '100%', 60) }}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}