The DOMTokenList
interface represents a set of space-separated tokens. Such a set is returned by Element.classList
, HTMLLinkElement.relList
, HTMLAnchorElement.relList
or HTMLAreaElement.relList
. It is indexed beginning with 0
as with JavaScript Array
objects. DOMTokenList
is always case-sensitive.
DOMTokenList.length
Read only
integer
representing the number of objects stored in the object.DOMTokenList.value
Read only
DOMString
.DOMTokenList.item()
DOMTokenList.contains()
true
if the list contains the given token, otherwise false
.DOMTokenList.add()
DOMTokenList.remove()
DOMTokenList.replace()
DOMTokenList.supports()
true
if a given token is in the associated attribute's supported tokens.DOMTokenList.toggle()
true
.DOMTokenList.entries()
iterator
allowing you to go through all key/value pairs contained in this object.DOMTokenList.forEach()
DOMTokenList
element.DOMTokenList.keys()
iterator
allowing you to go through all keys of the key/value pairs contained in this object.DOMTokenList.values()
iterator
allowing you to go through all values of the key/value pairs contained in this object.In the following simple example we retrieve the list of classes set on a <p>
element as a DOMTokenList
using Element.classList
, add a class using DOMTokenList.add()
, and then update the 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:
Methods that modify the DOMTokenList
(such as DOMTokenList.add()
) automatically trim any excess 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:
Specification | Status | Comment |
---|---|---|
Credential Management Level 1 | Editor's Draft | Adds the supports() method. |
DOM The definition of 'DOMTokenList' in that specification. | Living Standard | Initial definition |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 10 | (Yes) | (Yes) |
replace() | 61 | 49 (49) | ? | 48 | (Yes) |
Iterable methods (entries() , keys() , forEach() , values() ) | (Yes) | 50 (50) | ? | (Yes) | ? |
supports() | 50 | 49 (49) | No support | (Yes) | ? |
Remove whitespace and duplicates | (Yes) | 55 (55) | (Yes)[1] | (Yes) | (Yes) |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | ? | (Yes) |
replace() | 61 | 61 | 49.0 (49) | ? | 48 | (Yes) |
Iterable methods (entries() , keys() , forEach() , values() ) | ? | (Yes) | 50.0 (50) | ? | (Yes) | ? |
supports() | 50 | 50 | 49.0 (49) | No support | No support | No support |
Remove whitespace and duplicates | ? | (Yes) | 55.0 (55) | (Yes)[1] | (Yes) | (Yes) |
[1] IE only trims whitespace — it doesn't remove duplicates.
DOMSettableTokenList
(object that extends DOMTokenList
with settable .value property)
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList