Created
February 22, 2018 21:31
-
-
Save 6ui11em/8988f658d8a2306f6e2c62eb118e6bd4 to your computer and use it in GitHub Desktop.
Event listener javascript vanilla #js #vanilla #javascript
This file contains hidden or 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
// https://gomakethings.com/why-the-vanilla-js-matches-method-wont-work-with-event-listeners-and-nested-links/ | |
// closest to get element or parents | |
document.addEventListener('click', function (event) { | |
// If the clicked element doesn't have the class, bail | |
if (!event.target.closest('.click-me')) return; | |
// Otherwise, do whatever... | |
}, false); | |
// matches to get element | |
document.addEventListener('click', function (event) { | |
// If the clicked element doesn't have the class, bail | |
if (!event.target.matches('.click-me')) return; | |
// Otherwise, do whatever... | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment