473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get root node's or Element's id

Hi all,
on clicking the child node or element I need to get the id of the root element or node. id it possible, I have no idea

for example
<table>
<tr><td id="root">
<table>
<tr><td>
<table>
<tr><td id="child"> this is a <B>Test</B> </tr></td>
</table>
</tr></td>
</table>
</tr></td>
</table>

On clicking the element "child" i need to get the id of the element "root" is it possible . i need to traverse up the Tree I hope.


Im new to javascript.. if there is anythng worng pls inform me

Thanks in advance for guiding me
May 15 '07 #1
2 4525
iam_clint
1,208 Expert 1GB
example provided
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function getParents(obj) { 
  3.  objparent = obj.parentNode;
  4.  while (objparent) {
  5.    obj = objparent;
  6.    objparent = obj.parentNode; 
  7.    if (obj.id) {
  8.      alert(obj.id);
  9.    }
  10.  }
  11. }
  12. </script>
  13. <table>
  14. <tr><td id="root">
  15. <table>
  16. <tr><td>
  17. <table>
  18. <tr><td id="child" onclick="getParents(this);"> this is a <B>Test</B> </tr></td>
  19. </table>
  20. </tr></td>
  21. </table>
  22. </tr></td>
  23. </table>
  24.  
May 15 '07 #2
hi clint,
Cheers Thats Great !!!

But i need another favour too.......

This is Good but .. I require only a part of it
<div id = "testroot">
<table>
<tr><td id="root">
<table>
<tr><td>
<table>
<tr><td id="child" onclick="getParents(this);"> this is a <B>Test</B> </tr></td>
</table>
</tr></td>
</table>
</tr></td>
</table>
</div>

<div>
<table id="testroot2">
<tr><td id="root">
<table>
<tr><td>
<table>
<tr><td id="child2" onclick="getParents(this);"> this is a <B>Test</B> </tr></td>
</table>
</tr></td>
</table>
</tr></td>
</table>
</div>
<table>
<tr><td></td></tr>
..... more stmts
</table>

i need to traverse only up to div id="testroot1" or id="testroot2" and not more than that.

Let me explain the senario..

Onclicking a button (found a the child node) i need to initialize a function. for diff pages. unfortunately all those button tags dosent have an id or name attribute to it. and i cannot add the function manually for each button.

So i thought of traversing up the tree up to its div to identify the button and then by setting an id to the clicked button i can initialize the function globally for each button

Why I need only Up to the Div

The div id="testroot1" is inserted into a page from outside. The contents of the div is in a diff html file and the file is inserted depending on the click.
So as i feel the root of the id "child" is "testroot1". i need to move up only up to "testroot1". so that i can set the id of the clicked button with reference to the div
so that there are no futhere confusions in manupulating the ids.

Pls Let me know im new to these things.



Againg looking for some thig that could help me. But a Lot of thanks for the previous reply it was useful !!

-NewLearner
May 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Scott Simpson | last post by:
Can you query from a non-root node using XPathAPI's function selectNodeList(Node contextNode, java.lang.String str)? I'm trying this using the XPath expression "//*" and I'm getting nothing back. I...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
1
by: Brian | last post by:
Every time add data and save an xml document using XmlDataDocument.Save I get another root node added to the xml file. Am I doing something wrong or is this supposed to happen? Sample Code: ...
6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
1
by: SideByEach | last post by:
Is there any way to determine what would be the XML's root node in a XmlSchema class? I'm not sure if this is even possible since "XmlSchemaElement" objects don't have a parent element. I realize...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
11
by: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= | last post by:
at the page : <http://thoraval.yvon.free.fr/Fixed_layout/import_nodes.xhtml> when importing an svg document i have to do : document.importNode(...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.