How to correctly iterate through getElementsByClassName with JavaScript?

Spread the love

Sometimes, we want to correctly iterate through getElementsByClassName with JavaScript.

In this article, we’ll look at how to correctly iterate through getElementsByClassName with JavaScript.

How to correctly iterate through getElementsByClassName with JavaScript?

To correctly iterate through getElementsByClassName with JavaScript, we use a for-of loop.

For instance, we write

for (const li of listA.getElementsByClassName("li")) {
  //...
}

to get all li elements in the listA element with getElementsByClassName.

Then we use a for-of loop to loop through the returned entries in the node list.

Conclusion

To correctly iterate through getElementsByClassName with JavaScript, we use a for-of loop.

Leave a Reply

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