Skip to content

Instantly share code, notes, and snippets.

@6ui11em
Created February 22, 2018 21:31
Show Gist options
  • Save 6ui11em/8988f658d8a2306f6e2c62eb118e6bd4 to your computer and use it in GitHub Desktop.
Save 6ui11em/8988f658d8a2306f6e2c62eb118e6bd4 to your computer and use it in GitHub Desktop.
Event listener javascript vanilla #js #vanilla #javascript
// 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