Created
February 21, 2019 06:05
-
-
Save brandonros/f276b75099d363d8c74e00ec55892e91 to your computer and use it in GitHub Desktop.
Vanilla JavaScript equivalent of jQuery $('body').on('click', 'selector', ...)
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
const bindEvent = (eventNames, selector, handler) => { | |
eventNames.split(' ').forEach((eventName) => { | |
document.addEventListener(eventName, function (event) { | |
if (event.target.matches(selector + ', ' + selector + ' *')) { | |
handler.apply(event.target.closest(selector), arguments) | |
} | |
}, false) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment