Answer by user7607751 for adding multiple event listeners to one element
For those whom are looking for a jQuery solution, it could be done like this:$('#selector').on('click touchstart', function() { //do something here });Or:$('#selector').on('click touchstart', myFunction);
View ArticleAnswer by Sunil Xtha for adding multiple event listeners to one element
'onclick' in the html works for both touch and click event. Here's the example.
View ArticleAnswer by Dimitrios Tagaroulias for adding multiple event listeners to one...
I'm new at JavaScript coding, so forgive me if I'm wrong.I think you can create an object and the event handlers like this:const myEvents = { click: clickOnce, dblclick: clickTwice,};function...
View ArticleAnswer by Marco Concas for adding multiple event listeners to one element
You can simply do it iterating an Object. This can work with a single or multiple elements. This is an example:const ELEMENTS = {'click': element1, ...};for (const [key, value] of...
View ArticleAnswer by Hasan A Yousef for adding multiple event listeners to one element
What about something like this:['focusout','keydown'].forEach( function(evt) { self.slave.addEventListener(evt, function(event) { // Here `this` is for the slave, i.e. `self.slave` if ((event.type ===...
View ArticleAnswer by Nwaedobadoo for adding multiple event listeners to one element
//catch volume updatevar volEvents = "change,input";var volEventsArr = volEvents.split(",");for(var i = 0;i<volknob.length;i++) { for(var k=0;k<volEventsArr.length;k++) {...
View ArticleAnswer by Luis Febro for adding multiple event listeners to one element
This is my solution in which I deal with multiple events in my workflow.let h2 = document.querySelector("h2");function addMultipleEvents(eventsArray, targetElem, handler) {...
View ArticleAnswer by Armin for adding multiple event listeners to one element
Simplest solution for me was passing the code into a separate function and then calling that function in an event listener, works like a charm.function somefunction() { ..code goes here...
View ArticleAnswer by Vincent Tang for adding multiple event listeners to one element
Semi-related, but this is for initializing one unique event listener specific per element.You can use the slider to show the values in realtime, or check the console.On the <input> element I have...
View ArticleAnswer by Ian Pollak for adding multiple event listeners to one element
I just made this function (intentionally minified):((i,e,f)=>e.forEach(o=>i.addEventListener(o,f)))(element, events, handler)Usage:((i,e,f)=>e.forEach(o=>i.addEventListener(o,f)))(element,...
View ArticleAnswer by Strauss Bornman for adding multiple event listeners to one element
I have a small solution that attaches to the prototype EventTarget.prototype.addEventListeners = function(type, listener, options,extra) { let arr = type; if(typeof type == 'string'){ let sp =...
View ArticleAnswer by John Balvin Arias for adding multiple event listeners to one element
document.getElementById('first').addEventListener('touchstart',myFunction);document.getElementById('first').addEventListener('click',myFunction);function myFunction(e){...
View ArticleAnswer by Norair for adding multiple event listeners to one element
This mini javascript libary (1.3 KB) can do all these thingshttps://github.com/Norair1997/norjs/nor.event(["#first"], ["touchstart", "click"], [doSomething, doSomething]);
View ArticleAnswer by Allan Nienhuis for adding multiple event listeners to one element
I thought some might find this approach useful; it could be applied to any similarly repetitive code:ES6['click','ontouchstart'].forEach( evt => element.addEventListener(evt, dosomething,...
View ArticleAnswer by KooiInc for adding multiple event listeners to one element
Maybe you can use a helper function like this:// events and args should be of type Arrayfunction addMultipleListeners(element,events,handler,useCapture,args){ if (!(events instanceof Array)){ throw...
View ArticleAnswer by Torsten Walter for adding multiple event listeners to one element
For large numbers of events this might help:var element = document.getElementById("myId");var myEvents = "click touchstart touchend".split("");var handler = function (e) { do something};for (var i=0,...
View ArticleAnswer by Mattias Buelens for adding multiple event listeners to one element
Unless your do_something function actually does something with any given arguments, you can just pass it as the event handler.var first =...
View ArticleAnswer by Esailija for adding multiple event listeners to one element
You can just define a function and pass it. Anonymous functions are not special in any way, all functions can be passed around as values.var elem =...
View Articleadding multiple event listeners to one element
So my dilemma is that I don't want to write the same code twice. Once for the click event and another for the touchstart event.Here is the original...
View Article
More Pages to Explore .....