1. Home
  2. Javascript
  3. Add multiple event listeners

Example of looping through an array of objects and adding click event listeners.

#javascript#event#loop
var itemsArray = document.querySelectorAll('.item');

itemsArray.forEach(function(item){
    item.addEventListener('click', function(){
        console.log(item);
    });
});
copy
Full Javascript cheatsheet