Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 30, 2018 22:55
Show Gist options
  • Select an option

  • Save sbrl/c90dfe1e7de8cfb93fc8e3f54ce54c43 to your computer and use it in GitHub Desktop.

Select an option

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
/**
* 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