Nicc Nicc - 1 year ago 118
Javascript Question

Add eventListeners to each element from a NodeList

I want to add eventListener Click to each element of a NodeList. I've tried the following

var test = document.getElementsByClassName('productLnk')

function theTest (element,index,array){


It returns test.forEach is not a function

Answer Source

You need to convert NodeList to Array for iterating using Array#forEach method. In ES6 use Array.from method


or in older browser use Array#slice with Function#apply or Function#call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download