Skip to content

Instantly share code, notes, and snippets.

@ludder
Created November 9, 2012 11:36
Show Gist options
  • Save ludder/4045263 to your computer and use it in GitHub Desktop.
Save ludder/4045263 to your computer and use it in GitHub Desktop.
getParentByTagName - Get parent node for given tagname
/**
* Get parent node for given tagname
* @param {Object} node DOM node
* @param {String} tagname HTML tagName
* @return {Object} Parent node
*/
function getParentByTagName(node, tagname) {
var parent;
if (node === null || tagname === '') return;
parent = node.parentNode;
tagname = tagname.toUpperCase();
while (parent.tagName !== "HTML") {
if (parent.tagName === tagname) {
return parent;
}
parent = parent.parentNode;
}
return parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment