Skip to content

Instantly share code, notes, and snippets.

@GarrettS
Created September 4, 2014 00:02
Show Gist options
  • Select an option

  • Save GarrettS/1fb0709bb03e8d5bae25 to your computer and use it in GitHub Desktop.

Select an option

Save GarrettS/1fb0709bb03e8d5bae25 to your computer and use it in GitHub Desktop.

Revisions

  1. GarrettS created this gist Sep 4, 2014.
    18 changes: 18 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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;
    }