Skip to content

Instantly share code, notes, and snippets.

@andrewluetgers
Last active February 28, 2019 16:08
Show Gist options
  • Save andrewluetgers/027221b5f7a1cab22b637382c796385d to your computer and use it in GitHub Desktop.
Save andrewluetgers/027221b5f7a1cab22b637382c796385d to your computer and use it in GitHub Desktop.
Select a parent node that matches a query selector.
export default function selectParent(el, selector) {
let parent = null,
p = el.parentNode,
pp = p.parentNode,
sel = pp ? pp.querySelector(selector) : null,
done = !pp || sel === p;
console.log(selector, el)
return done ? sel : selectParent(p, selector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment