DEV Community

GreggHume
GreggHume

Posted on

4 3

Simplest way to loop node list / HTMLCollection using map, foreach etc

You spread the list like this:

const elements = document.getElementsByClassName('modal');

[...elements].forEach((element)=> {
  console.log(element)
})
Enter fullscreen mode Exit fullscreen mode

Or in one line like this, this is useful if you are doing multiple loops and do not want to spread every time:

const elements = [...document.getElementsByClassName('modal')];

elements.forEach((element)=> {
  console.log(element)
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more