Categories
JavaScript Answers

How to loop through childNodes with JavaScript?

Spread the love

Sometimes, we want to loop through childNodes with JavaScript.

In this article, we’ll look at how to loop through childNodes with JavaScript.

How to loop through childNodes with JavaScript?

To loop through childNodes with JavaScript, we can use a for-of loop.

For instance, we write

const children = element.childNodes;

for (const child of children) {
  console.log(child);
}

to get the child nodes of the element with the childNodes property.

Then we loop through the children child nodes with a for-of loop.

We get the node being looped through from child.

Conclusion

To loop through childNodes with JavaScript, we can use a for-of loop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *