Created
November 9, 2012 11:36
-
-
Save ludder/4045263 to your computer and use it in GitHub Desktop.
getParentByTagName - Get parent node for given tagname
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
/** | |
* 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