Created
August 13, 2022 18:05
-
-
Save jgoslow/68b5ea73ef34d4617ff1ede18c5c6b2b to your computer and use it in GitHub Desktop.
Ancestor Selection JS Helper Functions
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
function getAncestorByAttribute(elem, attribute) { | |
if (elem.parentElement === null) { | |
return false; | |
} else if (elem.hasAttribute(attribute)) { | |
return elem; | |
} else { | |
return getAncestorByAttribute(elem.parentElement, attribute); | |
} | |
} |
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
/** | |
* Ancestor Selection Helpers | |
*/ | |
function getAncestorByClass(elem, selector) { | |
if (elem.parentElement === null) { | |
return false; | |
} else if (elem.classList.contains(selector)) { | |
return elem; | |
} else { | |
return getAncestorByClass(elem.parentElement, selector); | |
} | |
} |
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
function getAncestorByTag(elem, tag) { | |
if (elem.parentElement === null) { | |
return false; | |
} else if (elem.tagName.toLowerCase() === tag.toLowerCase()) { | |
return elem; | |
} else { | |
return getAncestorByTag(elem.parentElement, tag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment