Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Find Ancestor Element Of A Tag Given An Object Reference

DZone's Guide to

Find Ancestor Element Of A Tag Given An Object Reference

·
Free Resource
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:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}