js addeventlistener multiple events code example
Example 1: 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();
});
Example 2: window.addeventlistener multiple events
"mousemove touchmove".split(" ").forEach(function(e){
window.addEventListener(e,mouseMoveHandler,false);
});
Example 3: 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
Example 4: multiple event with javascript
/* get the button id "tg" which is used for toggling.Then on click few
elements with classname "adv" will disappear and at the same time few elements
with classname "btnpro" will increase in width to 80px*. Upon second click
elements with "adv" will appear again and the button width changes to 60px*/
document.getElementById("tg").onclick = function ()
{
my_fun1();
my_fun2();
}
function my_fun1()
{
var x = document.getElementsByClassName("adv")
for(var i=0; i