javascript add event listenner for multiple events
/* Add one or more listeners to an element
** @param {DOMElement} element - DOM element to add listeners to
** @param {string} eventNames - space separated list of event names, e.g. 'click change'
** @param {Function} listener - function to attach for each event as a listener
*/functionaddListenerMulti(element, eventNames, listener){var events = eventNames.split(' ');for(var i=0, iLen=events.length; i<iLen; i++){
element.addEventListener(events[i], listener,false);}}addListenerMulti(window,'mousemove touchmove',function(){…});
var eventList =["change","keyup","paste","input","propertychange","..."];for(event of eventList){
element.addEventListener(event,function(){// your function body...console.log("you inserted things by paste or typing etc.");});}
constinvokeMe=()=>console.log('I live here outside the scope');constalsoInvokeMe=()=>console.log('I also live outside the scope');
element.addEventListener('event',()=>{invokeMe();alsoInvokeMe();});
// events and args should be of type ArrayfunctionaddMultipleListeners(element,events,handler,useCapture,args){if(!(events instanceofArray)){throw'addMultipleListeners: '+'please supply an array of eventstrings '+'(like ["click","mouseover"])';}//create a wrapper to be able to use additional argumentsvarhandlerFn=function(e){
handler.apply(this, args && args instanceofArray? args :[]);}for(var i=0;i<events.length;i+=1){
element.addEventListener(events[i],handlerFn,useCapture);}}functionhandler(e){// do things};// usageaddMultipleListeners(document.getElementById('first'),['touchstart','click'],
handler,false);
var eventList =["change","keyup","paste","input","propertychange","..."];for(event of eventList){
element.addEventListener(event,function(){// your function body...console.log("you inserted things by paste or typing etc.");});}