Skip to content

Instantly share code, notes, and snippets.

@ralt
Last active December 14, 2015 05:09
Show Gist options
  • Save ralt/5033490 to your computer and use it in GitHub Desktop.
Save ralt/5033490 to your computer and use it in GitHub Desktop.
Event delegation
function delegate(evt, parent, selector, fn) {
parent.addEventListener(evt, function(e) {
var elt = function find(el) {
if (el.matchesSelector(selector)) {
return el;
}
else {
if (el.parentNode !== parent) {
return find(el.parentNode);
}
return false;
}
}(e.target);
if (elt) {
fn.call(elt, e);
}
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment