Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add multiple event listeners

['click','ontouchstart'].forEach( function(evt) {
    element.addEventListener(evt, dosomething, false);
});
Comment

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
*/
function addListenerMulti(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(){…});
Comment

addeventlistener javascript multiple functions

const invokeMe = () => console.log('I live here outside the scope');
const alsoInvokeMe = () => console.log('I also live outside the scope'); 

element.addEventListener('event',() => {    
     invokeMe();
     alsoInvokeMe();    
});
Comment

addEventListener to multiple events

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.");
    });
}
Comment

javascript addeventlistener click multiple elements

let all_btn = document.querySelectorAll("button");
all_btn.forEach(function(btn) {
    btn.addEventListener("click", function() {
        console.log(this.innerHTML + " is clicked")
    });
});

// one line code
// let all_btn=document.querySelectorAll("button");all_btn.forEach(function(n){n.addEventListener("click",function(){console.log(this.innerHTML+" is clicked")})});
Comment

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);
Comment

window.addeventlistener multiple events

   "mousemove touchmove".split(" ").forEach(function(e){
      window.addEventListener(e,mouseMoveHandler,false);
    });
Comment

javascript add event listenner for multiple events

function addListenerMulti(el, s, fn) {
  s.split(' ').forEach(e => el.addEventListener(e, fn, false));
}
Comment

javascript add event listenner for multiple events

"mousemove touchmove".split(" ").forEach(function(e){
      window.addEventListener(e,mouseMoveHandler,false);
    });
Comment

listener to multiple elements

[ Element1, Element2 ].forEach(function(element) {
   element.addEventListener("input", function() {
      this function does stuff
   });
});
Comment

addEventListener to multiple events

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.");
    });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: export multiple function in node js 
Javascript :: boolean javascript 
Javascript :: wordpress get plugin url in javascript 
Javascript :: passing json as datasource to jasper report library 
Javascript :: strict type javascript 
Javascript :: decode jwt tokens 
Javascript :: delete node between indexes node list js 
Javascript :: javscript loop array 
Javascript :: ejs layout 
Javascript :: merge binary tree 
Javascript :: Send Data Using Express With Post 
Javascript :: Generate a random Id safely 
Javascript :: javascript force view to focus on a div 
Javascript :: javascript keylogger 
Javascript :: comparison operators in javascript 
Javascript :: react to pdf 
Javascript :: react footer component 
Javascript :: the filter array 
Javascript :: javscript call 
Javascript :: how to target two id using jquery 
Javascript :: click tester 
Javascript :: copy text to the clipboard 
Javascript :: mongoose max record 
Javascript :: limit number in javascript 
Javascript :: javascript allow only numbers in input alert 
Javascript :: js to find min value in an array 
Javascript :: Difference between “ == “ and “ === “ operators. 
Javascript :: lodash isNil 
Javascript :: javasccript this.innerHTML 
Javascript :: how to add css based on route react 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =