Skip to content

Instantly share code, notes, and snippets.

@queviva
Created February 1, 2022 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queviva/d4d56783e0849dbca791ade8a3e37c07 to your computer and use it in GitHub Desktop.
Save queviva/d4d56783e0849dbca791ade8a3e37c07 to your computer and use it in GitHub Desktop.
add|remove the same listener for multiple events
const Liz = {
lizzers: [
[obj, 'event', e=>{...}],
[obj, 'event,event,...', e=>{...}],
[obj, 'event,event,...', e=>{...},{ passive: true }]
],
add: function(L = this.lizzers, v = 0) {
for (let [x, y, z, p = { passive: false }] of L) {
y.split(',').forEach(e =>
x[['add', 'remove'][v] + 'EventListener'](e, z, p)
);
}
},
remove: function() { this.add(this.lizzers, 1); }
};
@queviva
Copy link
Author

queviva commented Feb 1, 2022

declaring all listeners inside this object allows you to add|remove the same handler for multiple events on the same element

calling Liz.add() adds every listener; Liz.remove() removes them all for a quick 'en|dis-able' method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment