Skip to content

Instantly share code, notes, and snippets.

@GarrettS
Created September 4, 2014 00:02
Show Gist options
  • Save GarrettS/1fb0709bb03e8d5bae25 to your computer and use it in GitHub Desktop.
Save GarrettS/1fb0709bb03e8d5bae25 to your computer and use it in GitHub Desktop.
Selectors: findAncestor
function matchesSelector(el, selectorText, ctx) {
var all = (ctx||el.ownerDocument).querySelectorAll(selectorText);
return indexOf(all, el) != -1;
}
function findAncestor(el, selectorText, container) {
if(el == null || el === container)
return null;
var parent = el.parentNode;
while(parent && parent != container) {
if(matchesSelector(parent, selectorText, container)) {
return parent;
}
parent = parent.parentNode;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment