Created
July 11, 2019 06:38
-
-
Save andreasvirkus/ed219aaf80f52cc53016cf5c31e774d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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