DOMTokenList: keys() method
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.
Thekeys() method of theDOMTokenList interfacereturns aniterator allowing to go through all keys contained in this object.The keys are unsigned integers.
In this article
Syntax
keys()Parameters
None.
Return value
Returns aniterator.
Examples
In the following example we retrieve the list of classes set on a<span> element as aDOMTokenList usingElement.classList. We then retrieve an iterator containing the keys usingkeys(),then iterate through those keys using afor...of loop,writing each one to the<span>'sNode.textContent.
First, the HTML:
<span></span>Now the #"span");const classes = span.classList;const iterator = classes.keys();for (let value of iterator) { span.textContent += `(${value}) `;}
The output looks like this: