HTML DOM NodeList forEach()

❮ The NodeList Object

Examples

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 the names of the document's child nodes:

const list = document.body.childNodes;

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.




Syntax

nodelist.forEach(function(currentValue, index, arr), thisValue)

Parameters

function() Required.
A function to run for each node.
currentValue Required.
The value of the current node.
index Optional.
The index of the current node.
arr Optional.
The NodeList of the current node.
thisValue Optional. Default 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 »

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

nodelist.forEach() is not supported in Internet Explorer 11 (or earlier).


❮ The NodeList Object
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.