Skip to content

Instantly share code, notes, and snippets.

@ralt
Created November 7, 2012 09:51
Show Gist options
  • Save ralt/4030481 to your computer and use it in GitHub Desktop.
Save ralt/4030481 to your computer and use it in GitHub Desktop.
Closest() rewritten
if (!Node.prototype.matchesSelector) {
Node.prototype.matchesSelector = function(selector) {
return this === document.querySelector(selector);
};
}
function closest(selector, el) {
if (el.parentNode.matchesSelector(selector)) {
return el.parentNode;
}
else {
// No TCO, but heh...
return closest(selector, el.parentNode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment