Skip to content

Instantly share code, notes, and snippets.

@think49
Created December 29, 2010 20:06
Show Gist options
  • Save think49/758999 to your computer and use it in GitHub Desktop.
Save think49/758999 to your computer and use it in GitHub Desktop.
getAncestorElementByTagName.js : 対象の tagName を持つ祖先要素ノードを返す。存在しなければ null を返す。
/**
* getAncestorElementByTagName.js
*
* @version 1.0
* @author think49
*/
/**
* get Ancestor Element by tagName.
* @function
* @param {Object} node DOM Node.
* @param {String} tagName Tag Name.
* @returns Ancestor element node. If a target node is not found, return null.
* @type {Object}, {Boolean}
*/
function getAncestorElementByTagName (node, tagName) {
if (typeof node !== 'object' || !node) {
return null;
}
tagName = (tagName + '').toLowerCase();
while (node && node.parentNode) {
node = node.parentNode;
if (node.tagName && node.tagName.toLowerCase() === tagName) {
break;
}
}
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment