Answers for "multiple event listeners on same element javascript"

11

add event listener to multiple element in JS

document.querySelectorAll('.some-class').forEach(item => {
  item.addEventListener('click', event => {
    //handle click
  })
})
Posted by: Guest on August-07-2020
1

same click event in multiple elements in on event

$(document).on("click touchend", ".class1, .class2, .class3", function () {
     //do stuff
});
Posted by: Guest on February-17-2021
0

how to add multiple event listener in javascript

// events and args should be of type Array
function addMultipleListeners(element,events,handler,useCapture,args){
  if (!(events instanceof Array)){
    throw 'addMultipleListeners: '+
          'please supply an array of eventstrings '+
          '(like ["click","mouseover"])';
  }
  //create a wrapper to be able to use additional arguments
  var handlerFn = function(e){
    handler.apply(this, args && args instanceof Array ? args : []);
  }
  for (var i=0;i<events.length;i+=1){
    element.addEventListener(events[i],handlerFn,useCapture);
  }
}

function handler(e) {
  // do things
};

// usage
addMultipleListeners(
    document.getElementById('first'),
    ['touchstart','click'],
    handler,
    false);
Posted by: Guest on July-10-2020

Code answers related to "multiple event listeners on same element javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language