Skip to content

Instantly share code, notes, and snippets.

@lazd
Created August 29, 2015 02:05
Show Gist options
  • Select an option

  • Save lazd/7d023fb808f1e9adf504 to your computer and use it in GitHub Desktop.

Select an option

Save lazd/7d023fb808f1e9adf504 to your computer and use it in GitHub Desktop.
function delegate(el, type, selector, callback) {
el = typeof el === 'string' ? document.querySelector(el) : el;
if (!el) return;
el.addEventListener(type, function(event) {
var node = event.target;
while (node && node !== el) {
if (node.matches(selector))
callback.call(node, event);
node = node.parentNode;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment