Reputation: 141
Is it possible to listen to all events that are triggered on a DOM element no matter the name of the event? And if its possible is there any reason one should no do that?
Unfortunately I was not able to find anything about that in either Stack Overflow or Google :(
I am planning to write a script that needs to respond to about two dozens different custom events and I was wondering if, instead of binding each event to the element I could just listen for all of them and then based on the event name, dynamically call a function..
Upvotes: 1
Views: 1170
Reputation: 56
You can't do it in the way you've suggested but this is a simple alternative:
const events = 'click mouseover mouseout';
events.split(' ').forEach(e => window.addEventListener(e, doStuff));
function doStuff(){...}
Upvotes: 2