Skip to content

Instantly share code, notes, and snippets.

@ralt
Created November 7, 2012 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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