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 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

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

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 :: js id style backgroundColor change 
Javascript :: react native textinput turnoff capitalize first letter 
Javascript :: activate es6 module node 
Javascript :: check device type in javascript 
Javascript :: get height component react 
Javascript :: check textbox is empty in jquery 
Javascript :: how to get iso date with moment 
Javascript :: replacing characters in string javascript 
Javascript :: insertion sort javascript 
Javascript :: send a message when a bot joins your server discord.js 
Javascript :: jquery get attribute value of parent element 
Javascript :: javascript regex number only 
Javascript :: js check if value is not empty string 
Javascript :: Add table row in jQuery 
Javascript :: add element to body javascript 
Javascript :: mysql innodb_buffer_pool_size 
Javascript :: javascript get last character in string 
Javascript :: javascript to mask email address 
Javascript :: aos react 
Javascript :: enable native bracket matching vs cide 
Javascript :: javascript set input field value 
Javascript :: image is not displaying in react js 
Javascript :: js self executing anonymous function 
Javascript :: get scroll position jquery 
Javascript :: how to center a canvas in javascript 
Javascript :: js json download 
Javascript :: javascript getelementbyid disable 
Javascript :: javascript one off event listener 
Javascript :: document ready without jquery 
Javascript :: jquery redirect to another webpage 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =