Skip to content

Instantly share code, notes, and snippets.

@mehmetkurt
Forked from evenicoulddoit/js-delegate.js
Created September 23, 2021 00:54
Show Gist options
  • Save mehmetkurt/c82a2ec736afd3f40df7b624ef492032 to your computer and use it in GitHub Desktop.
Save mehmetkurt/c82a2ec736afd3f40df7b624ef492032 to your computer and use it in GitHub Desktop.
Basic JavaScript Delegation
(function(document, EventTarget) {
var elementProto = window.Element.prototype,
matchesFn = elementProto.matches;
/* Check various vendor-prefixed versions of Element.matches */
if(!matchesFn) {
['webkit', 'ms', 'moz'].some(function(prefix) {
var prefixedFn = prefix + 'MatchesSelector';
if(elementProto.hasOwnProperty(prefixedFn)) {
matchesFn = elementProto[prefixedFn];
return true;
}
});
}
/* Traverse DOM from event target up to parent, searching for selector */
function passedThrough(event, selector, stopAt) {
var currentNode = event.target;
while(true) {
if(matchesFn.call(currentNode, selector)) {
return currentNode;
}
else if(currentNode != stopAt && currentNode != document.body) {
currentNode = currentNode.parentNode;
}
else {
return false;
}
}
}
/* Extend the EventTarget prototype to add a delegateEventListener() event */
EventTarget.prototype.delegateEventListener = function(eName, toFind, fn) {
this.addEventListener(eName, function(event) {
var found = passedThrough(event, toFind, event.currentTarget);
if(found) {
// Execute the callback with the context set to the found element
// jQuery goes way further, it even has it's own event object
fn.call(found, event);
}
});
};
}(window.document, window.EventTarget || window.Element));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment