Search for an element UP the DOM tree
Login or Register to Bookmark this snippet
This function takes an argument (id as string or object reference) and the tag name of an element somewhere up the dom tree. It returns the element, or the body element if the element was not found.
Requires xGetElementById (also available on this site).
function searchUp(elm,findElm) {
//this function searches the dom tree upwards for the findElm node starting from elm.
//check if elm is reference
if(typeof(elm) == 'string') {
elm = xGetElementById(elm);
}
//search up
//get the parent findElm
while (elm.nodeName.toLowerCase() != findElm && elm.nodeName.toLowerCase() != 'body')
elm = elm.parentNode;
return elm;
}
Added by JC on 11th November, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment