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 :: change node version 
Javascript :: regular expressions password contains number 
Javascript :: Animated: `useNativeDriver` was not specified. This is a required option and must be explicitly set to `true` or `false` 
Javascript :: npm chalk 
Javascript :: javascript interval fixed number of times 
Javascript :: jspdf pdf from html 
Javascript :: how to clone array in js 
Javascript :: foreach loop javascript 
Javascript :: react native strike through text 
Javascript :: react hook form with yup resolver 
Javascript :: append row javascript 
Javascript :: sleeping in js 
Javascript :: get value of radio button javascript 
Javascript :: regex for username 
Javascript :: for of loop javascript 
Javascript :: store data to the browser’s localStorage 
Javascript :: change color js 
Javascript :: django pass list to javascript 
Javascript :: when i click on one checkbox check all checkboxes 
Javascript :: javascript add text to li 
Javascript :: Close popup window 
Javascript :: if classlist contains js 
Javascript :: math ceil 
Javascript :: how to find last element of array react 
Javascript :: add text to string javascript 
Javascript :: javascript kill ajax request 
Javascript :: toggle boolean js 
Javascript :: nodejs buffer.from base64 
Javascript :: how to manage a db connection in javascript 
Javascript :: pipe data to json angular 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =