Last active
January 30, 2018 22:55
-
-
Save sbrl/c90dfe1e7de8cfb93fc8e3f54ce54c43 to your computer and use it in GitHub Desktop.
[GetContainingElement] Traverses up the DOM tree to find the first containing parent element of a given type for a given child element. #dom #search #find
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
| /** | |
| * Gets the first containing parent element of a given type for a given child element. | |
| * @param {HTMLElement} element The child element to find the parent of. | |
| * @param {string} type The name of the element containing parent to search for. | |
| */ | |
| function GetContainingElement(element, type) | |
| { | |
| while(element !== null && element.tagName.toLowerCase() !== type.toLowerCase()) { | |
| element = element.parentNode; | |
| } | |
| return element; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment