474,045 Members | 43,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem traversing through dom

85 New Member
I am trying to get the values of all the child elements from a specific element

Expand|Select|Wrap|Line Numbers
  1. function getnodes(elem)
  2. {
  3.     for (i=0; i<elem.childNodes.length; i++)
  4.     {
  5.         alert(elem.childNodes[i].nodeName);
  6.         alert(elem.childNodes[i].nodeType);
  7.  
  8.         if(elem.childNodes[i].nodeType==1)
  9.         {
  10.             alert("hello - "+elem.childNodes[i].nodeName);
  11.             getnodes(elem.childNodes[i]);
  12.         }
  13.         else if(elem.childNodes[i].nodeType==3)
  14.         {
  15.             alert("hi text");
  16.         }
  17.     }
  18. }
  19. window.onload=function(){
  20.     var node=document.getElementById("hello");
  21.     var count=0;
  22.     while(count<5)
  23.     {
  24.         node=node.parentNode;
  25.         count++;
  26.     }
  27.     getnodes(node);
  28. }
this is the html code

[HTML]<div style="padding-left:15px; color:#0f0f0f; background-color:#0099FF"> <strong>hello </strong>
<table>
<tr>
<td bgcolor="#CC996 6"><iframe id="hello" src="http://www.xyz.com"></iframe></td>
</tr>
</table>
</div> [/HTML]

I am getting problems in the code

when i put my code directly in a new page, it is working fine but when i put it in some page, it gave only 2 alerts and completed without errors
the alerts were for tags <strong> then for #text and it completed
it never went to my table element
Feb 1 '08 #1
11 1564
gits
5,390 Recognized Expert Moderator Expert
hi ...

you use a strange method to refer to node ... which node do you really want to refer ... the table? or the div? ... may be it behaves different in IE? i think IE will count the table's tbody too so it may be a problem to use a fixed counter ... give the nodes that should be refered an id or a specific class to identify them reliably ...

kind regards
Feb 1 '08 #2
rohitchawla
85 New Member
well i am selecting a node and then traversing to its parent a fixed number of times
like if i select an element div with myid in <span><div><d iv id = "myid">
and then go up 2 times and ill get span element.
from that span element i want to get all the child nodes
i am back tracking to span from my div element
Feb 1 '08 #3
rohitchawla
85 New Member
the main reason i am doing this is to get the css styles of the parent elements
i dont have control of the top element

i just have a script that creates a div and i want to backtrack a fixed number of element and from then on get all the css styles of each child element
Feb 1 '08 #4
gits
5,390 Recognized Expert Moderator Expert
but as you can see it is not reliable ... so you could climb up recursivly and check what element you currently have refered ... and in case you have the right one then retrieve its styles ... a fixed number seems not to be correct in all cases!?

kind regards
Feb 1 '08 #5
rohitchawla
85 New Member
i tried doing that but by traversing up i could only get my current node's sibling's and its parent's sibling's css but not of their children
Feb 1 '08 #6
gits
5,390 Recognized Expert Moderator Expert
... hmmm i think i don't get it ... you could go the correct node? in case you can, then retrieve its children ... what error do you get ... and what code did you use?

kind regards
Feb 1 '08 #7
rohitchawla
85 New Member
well i was not getting any errors in the code
it just stopped at some nodes and didnt go ahead

i tried to make another code but im facing same kind of problem in this code as well
maybe i am not getting it to work recursively
Expand|Select|Wrap|Line Numbers
  1. function nodeTrav(elem)
  2. {
  3.     while(elem)
  4.     {
  5.         if(elem.nodeType==1)
  6.         {
  7.             alert("parent node is:"+elem.parentNode.nodeName+" current node is:"+elem.nodeName);
  8.             for(i=0;i<elem.childNodes.length;i++)
  9.             nodeTrav(elem.childNodes[i]);
  10.         }
  11.         elem=elem.nextSibling;
  12.     }
  13. }
  14. var x= document.getElementsByTagName("div")[0];
  15. nodeTrav(x);
Feb 6 '08 #8
Dasty
101 Recognized Expert New Member
You are using recursive calling of function, but you forgot to declare variable "i" as local variable, so of course code will mess up because all called functions in recursion share the same global "i" variable.

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<elem.childNodes.length;i++)
change it to:

Expand|Select|Wrap|Line Numbers
  1. for(var i=0;i<elem.childNodes.length;i++)
always check the variable's scope in recursion!
Feb 6 '08 #9
rohitchawla
85 New Member
thanks dasty
i changed the i to var i and atleast i went further deeper in the child elements but i was locked up in an infinite loop in some elements
Feb 6 '08 #10

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

Similar topics

1
4221
by: w.p. | last post by:
Hello! I want change default tab traversing in my app. But i don't know how to do it :( Belowe i include simple example - i want change default tab order: radiobutton "mode11" -> radiobutton "mode31" -> button OK I can't find any option, flag, or another way.
3
1975
by: Plamen Valtchev | last post by:
This is my problem: From JavaScript I want to find the list of all defined/loaded JavaScript functions/objects/names within the current scope (html page in a browser). the page could contain many included javascript with the script tag : <script language="javascript" src="XXX/Script.js"></script>
0
1120
by: thomson | last post by:
Hi all, can any one tell me which is fast traversing a XML file or a hash file is fast, i got few few field names and values in XML which i will use to retrieve. I can use Hash File also to do the same operation, Can any one tell me which is faster. Thanks in Advance thomson
2
2183
by: thomson | last post by:
Hi all, can any one tell me which is fast traversing a XML file or a hash file is fast, i got few few field names and values in XML which i will use to retrieve. I can use Hash File also to do the same operation, Can any one tell me which is faster. Thanks in Advance
4
4893
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the next for null).Is there any way to find the length of linked list in c.My need is to traverse from the end to 5th node Regards, Mani
30
4324
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
1545
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the query. The query comes from a table which picks values from another table. When I try traversing the query through TAB button, I get the following warning - "The value you entered isnt valid for this field. For example you might have entered a text...
19
3737
by: Jim | last post by:
Hi, I have two questions/problems pertaining to CSS horizontal dropdown menus and am hoping that someone here can help me out. (1) I'm having a problem centering the menu. I picked up the code for this from a tutorial but that menu was flush-left justified. Not what I want. Subsequent searches on google on how to center yielded a
12
4089
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in the shared memory. I run fedora 9 and gcc 4.2. I am able to insert values in to the list, remove values from the list, but the problem is in traversing the list. Atlease one or 2 list nodes disappear when traversing from the base of the list or...
9
3131
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
0
10337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
12023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6652
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6837
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.