--- title: DOMTokenList slug: Web/API/DOMTokenList tags: - API - DOM - DOMTokenList - Interface - Reference translation_of: Web/API/DOMTokenList ---
The DOMTokenList
interface represents a set of space-separated tokens. Such a set is returned by {{domxref("Element.classList")}}, {{domxref("HTMLLinkElement.relList")}}, {{domxref("HTMLAnchorElement.relList")}}, {{domxref("HTMLAreaElement.relList")}}, {{domxref("HTMLIframeElement.sandbox")}}, or {{domxref("HTMLOutputElement.htmlFor")}}. It is indexed beginning with 0
as with JavaScript {{jsxref("Array")}} objects. DOMTokenList
is always case-sensitive.
integer
representing the number of objects stored in the object.undefined
if the number is greater than or equal to the length of the list).true
if the list contains the given token, otherwise false
.true
if a given token is in the associated attribute's supported tokens.false
. If token doesn't exist it's added and the function returns true
.DOMTokenList
element.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:
var para = document.querySelector("p"); var classes = para.classList; para.classList.add("d"); para.textContent = 'paragraph classList is "' + classes + '"';
The output looks like this:
{{ EmbedLiveSample('Examples', '100%', 60) }}
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>
var span = document.querySelector("span"); var 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) }}
Specification | Status | Comment |
---|---|---|
{{SpecName("DOM WHATWG", "#interface-domtokenlist", "DOMTokenList")}} | {{Spec2("DOM WHATWG")}} | Initial definition |
{{Compat("api.DOMTokenList")}}
DOMTokenList
with settable .value property)