Last active
January 12, 2023 16:08
-
-
Save VinnyFonseca/6c1692c9ce3704b1a48ad401c22602e5 to your computer and use it in GitHub Desktop.
addEventListener multiple events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['change', 'keyup', 'paste', 'input', 'propertychange', 'every_other_event_you_want'].forEach(eventType => { | |
[...document.querySelectorAll('SELECTOR')].forEach(element => { | |
element.addEventListener(eventType, (event) => { | |
console.log(event); | |
// Your callback here | |
}, false); | |
}); | |
}); |
You're right, as I'm using querySelectorAll there should be a forEach after it. Don't forget to Array spread the element query. Amended.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the script, however it gets an error. Here is the correct version.