Multiple triggers for a single addEventListener function?
Is it possible to have multiple triggers for the same addEventListener function?
in jQuery, I can do something like this…
$(".selector").bind( "click keypress", function () {
// do this when any of these event triggers
// happen to the specified selector(s)...
});
It's a lot DRYer than repeating the same code over and over for each potential trigger when the outcome of the event should be the same.
Also, is it possible to add an event listener to all objects with a common selector of some sort, like the className or something?
To step it up a notch more, we could even do this and use multiple selectors with multiple event triggers… not sure how often that would happen, but both options are nice to have.
$('selectorOne','selectorTwo','etcThree').bind( "click keypress", function () {
// do this when any of these event triggers
// happen to the specified selector(s)...
});
1 Answer
-
Hi Trevor,
I'am afraid, we are not in the jQuery-world ;-)
Untestet:
var elems = [view1,view2,button1]; var events = ['touch','singletap','myownevent']; for (var el=0;el<elems.length;el++) { for (var ev=0;ev<events.length;ev++) { elems[i].addEventListener(events[ev],callbackfunction); } }
Hope it helps.
Rainer