The forEach()
method of the DOMTokenList
interface calls the callback given in parameter once for each value pair in the list, in insertion order.
The forEach()
method of the DOMTokenList
interface calls the callback given in parameter once for each value pair in the list, in insertion order.
js
forEach(callback) forEach(callback, thisArg)
callback
The function to execute for each element, eventually taking three arguments:
currentValue
The current element being processed in the array.
currentIndex
The index of the current element being processed in the array.
listObj
The array that forEach()
is being applied to.
thisArg
Optional
The value to use as this
when executing callback
.
None.
In the following example we retrieve the list of classes set on a <pre>
element as a DOMTokenList
using Element.classList
. We when retrieve an iterator containing the values using forEach()
, writing each one to the <pre>
's Node.textContent
inside the forEach()
inner function.
html
<pre class="a b c"></pre>
js
const pre = document.querySelector("pre"); const classes = pre.classList; const iterator = classes.values(); classes.forEach(function (value, key, listObj) { pre.textContent += `(${value}${key})/${this}\n`; }, "arg");
No specification data found for api.DOMTokenList.forEach
.
Check for problems with this page or contribute a missing spec_url
to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
forEach |
42 | 16 | 50 | No | 29 | 10.1 | 42 | 42 | 50 | 29 | 10.3 | 4.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/forEach