Over a million developers have joined DZone.

Find Ancestor Element Of A Tag Given An Object Reference

·
Retrieves the ancestor element of a tag given an object reference.


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
function findAncestor(o, tag){
	for(tag = tag.toLowerCase(); o = o.parentNode;)
		if(o.tagName && o.tagName.toLowerCase() == tag)
			return o;
	return null;
}
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}