Skip to content

Instantly share code, notes, and snippets.

@potch
Last active August 29, 2015 14:01
Show Gist options
  • Save potch/ebcf5916887963125175 to your computer and use it in GitHub Desktop.
Save potch/ebcf5916887963125175 to your computer and use it in GitHub Desktop.
function delegate(selector, handler) {
return function(e) {
var context = this;
var target = e.target;
var delegateEl = e.currentTarget;
var matches = delegateEl.querySelectorAll(selector);
for (var el = target; el.parentNode && el !== delegateEl; el = el.parentNode) {
for (var i = 0; i < matches.length; i++) {
if (matches[i] === el) {
handler.call(context, e);
return;
}
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment