Skip to content

Instantly share code, notes, and snippets.

@brandonros
Created February 21, 2019 06:05
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 brandonros/f276b75099d363d8c74e00ec55892e91 to your computer and use it in GitHub Desktop.
Save brandonros/f276b75099d363d8c74e00ec55892e91 to your computer and use it in GitHub Desktop.
Vanilla JavaScript equivalent of jQuery $('body').on('click', 'selector', ...)
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