Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save Alex1990/cc34fbc38bde0a7abf62 to your computer and use it in GitHub Desktop.

Select an option

Save Alex1990/cc34fbc38bde0a7abf62 to your computer and use it in GitHub Desktop.
A simple event delegation.
/**
* A simple event delegation method.
*/
// closest method: https://gist.github.com/Alex1990/5547956babc593852c3a
function delegate(elem, type, selector, listener, capture) {
listener._delegateWrapper = function(e) {
if (e.delegateTarget = closest(e.target, selector, elem)) {
listener.call(e.delegateTarget, e);
}
}
addEvent(elem, type, listener._delegateWrapper, capture);
}
function undelegate(elem, type, listener, capture) {
removeEvent(elem, type, listener._delegateWrapper, capture);
delete listener._delegateWrapper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment