Adding event listeners to multiple elements

Adding event listeners to multiple elements
0.0 0

#1

If you want to add an event listener to several controls you have to…
object.addEventListener("click", myScript);
So do you have to get the obj collection and loop over it?


#2

I did this in the end which seems to do the trick:

    var modals = document.getElementsByName("deleteBtn");
    if (modals) {
      for (var i=0; i < modals.length; i++) {
        modals[i].addEventListener("click", showModal);
      }
    };

#3

Didn’t need that code in the end. I passed (this) as a parameter in the onclick function to get access to the element attributes.