HTML DOM NodeList forEach()
❮ The NodeList ObjectExamples
Execute a function for each of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node, index) {
text += index + " " + node;
}
);
Try it Yourself »
list.forEach(
function(node, index) {
text += index + " " + node;
}
);
List the names of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeName;
}
);
list.forEach(
function(node) {
text += node.nodeName;
}
);
More examples below.
Try it Yourself »Description
The forEach() method executes a callback function for each node in a
NodeList.
See Also:
Syntax
nodelist.forEach(function(currentValue, index, arr), thisValue)
Parameters
function()
Required.
A function to run for each node.
A function to run for each node.
currentValue
Required.
The value of the current node.
The value of the current node.
index
Optional.
The index of the current node.
The index of the current node.
arr
Optional.
The NodeList of the current node.
The NodeList of the current node.
thisValue
Optional. Default
A value passed to the function as its
undefined.A value passed to the function as its
this value.
Return Value
NONE
More Examples
Example
List the types of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeType;
}
);
Try it Yourself »
list.forEach(
function(node) {
text += node.nodeType;
}
);
Browser Support
nodelist.forEach() is a DOM Level 4 (2015) feature.
It is supported in all modern browsers:
| Chrome | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes |
❮ The NodeList Object