Need to find a div's container div's classname and id

B

benjamin

I'm new at JS and DOM, so please bear with me... Here's what I'm trying
to do:

My code is such as this:

***
<div class="post" id="123">
lorem ipsum
<div class="tag1">hello</div>
</div>
***

I'm trying to figure out a function that would spit out the id of every
div that contains the "tag1" div.

I tried the following, by changing the "hello" line in the code above
to:
<div class="tag1" id="456" onClick="saymyclass('456')">this is the tag
text</div>

***
function saymyclass(id){
identity=document.getElementById(id);
alert(identity.className);
alert(identity.parentElement.className);
}
***

The first msg box returns, like expected, "tag1", but the second one
throws this error:
***
Error: identity.parentElement has no properties
***

Any help would be GREATLY appreciated!!
Thanks
 
W

web.dev

benjamin said:
I'm new at JS and DOM, so please bear with me... Here's what I'm trying
to do:

My code is such as this:

***
<div class="post" id="123">
lorem ipsum
<div class="tag1">hello</div>
</div>
***

I'm trying to figure out a function that would spit out the id of every
div that contains the "tag1" div.

I tried the following, by changing the "hello" line in the code above
to:
<div class="tag1" id="456" onClick="saymyclass('456')">this is the tag
text</div>

***
function saymyclass(id){
identity=document.getElementById(id);
alert(identity.className);
alert(identity.parentElement.className);

Should be instead:

identity.parentNode.className
 
B

benjamin

NEVERMIND...
just discovered it was a Firefox/IE deal... I am now using parentNode
instead of parentElement, and it works...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top