473,385 Members | 2,044 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.

problem traversing through dom

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="#CC9966"><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 1525
gits
5,390 Expert Mod 4TB
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
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><div 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
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 Expert Mod 4TB
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
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 Expert Mod 4TB
... 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
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 Expert 100+
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
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
Dasty
101 Expert 100+
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
Maybe because the recursion is not written right. You got two cycles for the elements on the same level (for & while). Just remove one of them (i removed "for") :D not sure if that caused infinity loop

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.  
INTO:

Expand|Select|Wrap|Line Numbers
  1. function nodeTrav(elem)
  2. {
  3.     while(elem)
  4.     {
  5.         if(elem.nodeType==1)
  6.         {
  7.             if (elem.childNodes.length > 0) nodeTrav(elem.firstChild);
  8.         }
  9.         elem=elem.nextSibling;
  10.     }
  11. }
  12.  
If this is not the case, can you post link to your page that is causing problem?
Feb 6 '08 #11
thank you very much dasty

it worked just perfectly
and now i know where i made it wrong

i just invoked every child of an element and used elem=elem.nextSibling which made me go through all the elements over and over again
Feb 7 '08 #12

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

Similar topics

1
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...
3
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...
0
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...
2
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...
4
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...
30
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
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...
19
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...
12
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...
9
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.