Categories
JavaScript Answers

How to correctly iterate through getElementsByClassName with JavaScript?

Spread the love

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

For instance, we write

const slides = document.getElementsByClassName("slide");
for (const slide of slides) {
  console.log(slide);
}

to call getElementsByClassName to select all elements with class slide.

Then we use a for-of loop to loop through all the slides and log its value.

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 *