1document.querySelectorAll('.some-class').forEach(item => {
2 item.addEventListener('click', event => {
3 //handle click
4 })
5})
1// events and args should be of type Array
2function addMultipleListeners(element,events,handler,useCapture,args){
3 if (!(events instanceof Array)){
4 throw 'addMultipleListeners: '+
5 'please supply an array of eventstrings '+
6 '(like ["click","mouseover"])';
7 }
8 //create a wrapper to be able to use additional arguments
9 var handlerFn = function(e){
10 handler.apply(this, args && args instanceof Array ? args : []);
11 }
12 for (var i=0;i<events.length;i+=1){
13 element.addEventListener(events[i],handlerFn,useCapture);
14 }
15}
16
17function handler(e) {
18 // do things
19};
20
21// usage
22addMultipleListeners(
23 document.getElementById('first'),
24 ['touchstart','click'],
25 handler,
26 false);
1/* get the button id "tg" which is used for toggling.Then on click few
2elements with classname "adv" will disappear and at the same time few elements
3with classname "btnpro" will increase in width to 80px*. Upon second click
4elements with "adv" will appear again and the button width changes to 60px*/
5
6document.getElementById("tg").onclick = function ()
7{
8 my_fun1();
9 my_fun2();
10}
11
12function my_fun1()
13{
14 var x = document.getElementsByClassName("adv")
15 for(var i=0; i<x.length;i++) {
16 if (x[i].style.display === "none") {
17 x[i].style.display = "block"
18 flag = false
19 } else {
20 x[i].style.display = "none"
21 flag = true
22 }
23 }
24}
25
26function my_fun2()
27{
28 var x = document.getElementsByClassName("btnpro")
29 for(var i=0; i<x.length;i++) {
30 if(flag){
31 x[i].style.width = "80px";
32 }else{
33 x[i].style.width = "60px";
34 }
35 }
36}
1document.querySelectorAll('.some-class').forEach(item => {
2 item.addEventListener('click', event => {
3 //handle click
4 })
5})
6
1const invokeMe = () => console.log('I live here outside the scope');
2const alsoInvokeMe = () => console.log('I also live outside the scope');
3
4element.addEventListener('event',() => {
5 invokeMe();
6 alsoInvokeMe();
7});
8