I'm attempting to get all elements by the class of 'element' and adding an event listener to each of them. however, getElementsByClassName()
returns and html collection. When I use .length()
it equals zero. how do I iterate through this collection in order to add event listener to each element in this class? is there a way to convert this html collection into an array?
here's what i have so far:
var tableElements = document.getElementsByClassName("element");
console.log(tableElements);
console.log(tableElements.length);
for(var i=0;i<tableElements.length;i++){
tableElements[i].addEventListener("click", dispElementData(this));
} //doesnt work