HTML - DOMTokenList forEach() Method



HTML DOMTokenList forEach() method calls the callback function mentioned in parameter once for each value pair in the list in the order of their insertion.

Syntax

domtokenlist.forEach(callback(currentValue, currentIndex, listObj), thisArg);

Parameter

This method accepts five parameters all of them are listed below.

Parameter Description
callback It is a required parameter. It represents a function to be executed on each element of nodelist. It accepts three parameters.
currentValue It is a required parameter. It represents current token in array.
currentIndex It is an optional parameter. It represents index of the current token in array.
listObj It is an optional parameter. It represents the array or DOMTokenList on which forEach() method is being applied to.
thisArg It is an optional parameter. It represents value to use as this while executing callback function.

Return Value

It does not return anything.

Example of HTML DOMTokenList 'forEach()' Method

Following example will illustrate the use of HTML DOMTokenList forEach() method.

<!DOCTYPE html>
<html>
<head>
    <title>
      HTML DOMTokenList forEach() Method
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <br>
    <span id="itr" class="itr1 itr2 itr3"></span>
    <script>
        function fun() {
            let x = document.getElementById("itr").classList;
            let result="";
            x.forEach(
                function (currentValue, currentIndex) {
                    result += currentIndex + ": " + currentValue +"<br>";
                }
            );
            document.getElementById("itr").innerHTML =result;
        }
    </script>
</body>
</html>

Supported Browsers

Method Chrome Edge Firefox Safari Opera
forEach() Yes 42 Yes 16 Yes 50 Yes 10.1 Yes 29
html_domtokenlist_reference.htm
Advertisements