Closed Thread Icon

Topic awaiting preservation: Help with parent and children Pages that link to <a href="https://ozoneasylum.com/backlink?for=8492" title="Pages that link to Topic awaiting preservation: Help with parent and children" rel="nofollow" >Topic awaiting preservation: Help with parent and children\

 
Author Thread
Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 01-27-2003 14:29

Say I was trying to get the a parent of a <span> tag, which is a TR tag. However, this span is in a TD tag. How do I get the parent of the TD tag from the span tag?

[This message has been edited by Hebedee (edited 01-27-2003).]

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 01-27-2003 14:46

Errrrm... I'm not so sure if I really understood what you are trying to do, but if you had something like this:

<tr>
&nbsp;&nbsp; <td>
&nbsp;&nbsp;&nbsp;&nbsp; <span id="myspan">blah</span>
&nbsp;&nbsp; </td>
</tr>

and wanted to access the <tr> you'd say

alert(document.getElementById("myspan").parentNode.parentNode.tagName);

to get its tag name or anything else you wanted to know.

Was that what you were looking for?

[This message has been edited by kuckus (edited 01-27-2003).]

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 01-27-2003 15:11

instead of just "parent" it is "parentNode"? Just clarifying, because I got it to work.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-27-2003 15:11

Kuckus is right. Now, let's say that you had the span element:

var myspan = document.getElementById('myspan');

and you knew that it was within a TR, but you didn't know how far down it was. (For instance, it could be in a TD, and within that it might be within a DIV, and within that it might be in a P, etc.) You could use this bit of code to find the first TR that is an ancestor (higher-level element) of it:

var theTR = myspan;
while (theTR.tagName != "TR" && theTR.parentNode)
{
theTR = theTR.parentNode;
}

This will make the variable TR move up the tree until it becomes a reference to a TR element, or until it hits the top level of the tree (the HTML tag).

« BackwardsOnwards »

Show Forum Drop Down Menu