Created
October 7, 2022 14:10
-
-
Save adamliptrot-oc/5457e9f913ed13e9ad26592ed1a01f95 to your computer and use it in GitHub Desktop.
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
// Find first ancestor of el with tagName | |
// or undefined if not found | |
function upTo(el, tagName) { | |
tagName = tagName.toLowerCase(); | |
while (el && el.parentNode) { | |
el = el.parentNode; | |
if (el.tagName && el.tagName.toLowerCase() == tagName) { | |
return el; | |
} | |
} | |
// Many DOM methods return null if they don't | |
// find the element they are searching for | |
// It would be OK to omit the following and just | |
// return undefined | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment