/    Sign up×
Community /Pin to ProfileBookmark

DOM: Get the outer most element of a child

Hi

I been trying to get the outer most element of a child without success and hope somebody here can help me out.

Let’s say I have the following structure:

[code=html]
<div>
Some text
</div>
<div>
<div>
<div>
<img src=”image.png” alt=”image” id=”image”/>
</div>
</div>
</div>
[/code]

I want to traverse to the element at level 1 from img/@id=image, but I do not know if the div is a child of another element. Eg. I want to get the root to the img element.

Thanks in advance for code/tips,

regards

t

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoAug 31.2008 — Just use a while loop to check for when the parentNode!=document.body . The object that has the parentNode of document.body is the outer most object:

&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
function getHighest(){
var obj = document.getElementById('img_check');
var par = obj.parentNode;

while(par!=document.body)
{
obj = par;
par = obj.parentNode;
}

alert(obj.id);
return;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="getHighest()"&gt;

&lt;div&gt;
Some text
&lt;/div&gt;
&lt;div id="outer_most"&gt;
&lt;div&gt;
&lt;div&gt;
&lt;img src="image.png" alt="image" id="img_check"/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @thoande spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...