javascript multiple listeners on same event code example
Example 1: window.addeventlistener multiple events
"mousemove touchmove".split(" ").forEach(function(e){
window.addEventListener(e,mouseMoveHandler,false);
});
Example 2: how to add multiple event listener in javascript
function addMultipleListeners(element,events,handler,useCapture,args){
if (!(events instanceof Array)){
throw 'addMultipleListeners: '+
'please supply an array of eventstrings '+
'(like ["click","mouseover"])';
}
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) {
};
addMultipleListeners(
document.getElementById('first'),
['touchstart','click'],
handler,
false);
Example 3: same click event in multiple elements in on event
$(document).on("click touchend", ".class1, .class2, .class3", function () {
});
Example 4: multiple event with javascript
document.getElementById("tg").onclick = function ()
{
my_fun1();
my_fun2();
}
function my_fun1()
{
var x = document.getElementsByClassName("adv")
for(var i=0; i<x.length;i++) {
if (x[i].style.display === "none") {
x[i].style.display = "block"
flag = false
} else {
x[i].style.display = "none"
flag = true
}
}
}
function my_fun2()
{
var x = document.getElementsByClassName("btnpro")
for(var i=0; i<x.length;i++) {
if(flag){
x[i].style.width = "80px";
}else{
x[i].style.width = "60px";
}
}
}