Created
September 4, 2014 00:02
-
-
Save GarrettS/1fb0709bb03e8d5bae25 to your computer and use it in GitHub Desktop.
Selectors: findAncestor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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