Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created July 11, 2019 06:38
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 andreasvirkus/ed219aaf80f52cc53016cf5c31e774d7 to your computer and use it in GitHub Desktop.
Save andreasvirkus/ed219aaf80f52cc53016cf5c31e774d7 to your computer and use it in GitHub Desktop.
const firstAncestor = (el, tagName) => {
while(el = el.parentNode && (el.tagName !== tagName.toUpperCase()))
return el
}
// Usage
var child = document.getElementById('child')
firstAncestor(child, 'div'); //<div id="parent">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment