problem traversing through dom

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitchawla
    New Member
    • Jul 2007
    • 85

    problem traversing through dom

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

    [CODE=javascript]
    function getnodes(elem)
    {
    for (i=0; i<elem.childNod es.length; i++)
    {
    alert(elem.chil dNodes[i].nodeName);
    alert(elem.chil dNodes[i].nodeType);

    if(elem.childNo des[i].nodeType==1)
    {
    alert("hello - "+elem.childNod es[i].nodeName);
    getnodes(elem.c hildNodes[i]);
    }
    else if(elem.childNo des[i].nodeType==3)
    {
    alert("hi text");
    }
    }
    }
    window.onload=f unction(){
    var node=document.g etElementById(" hello");
    var count=0;
    while(count<5)
    {
    node=node.paren tNode;
    count++;
    }
    getnodes(node);
    }[/CODE]
    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
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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

    Comment

    • rohitchawla
      New Member
      • Jul 2007
      • 85

      #3
      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

      Comment

      • rohitchawla
        New Member
        • Jul 2007
        • 85

        #4
        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

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          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

          Comment

          • rohitchawla
            New Member
            • Jul 2007
            • 85

            #6
            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

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              ... 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

              Comment

              • rohitchawla
                New Member
                • Jul 2007
                • 85

                #8
                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
                [CODE=javascript]function nodeTrav(elem)
                {
                while(elem)
                {
                if(elem.nodeTyp e==1)
                {
                alert("parent node is:"+elem.paren tNode.nodeName+ " current node is:"+elem.nodeN ame);
                for(i=0;i<elem. childNodes.leng th;i++)
                nodeTrav(elem.c hildNodes[i]);
                }
                elem=elem.nextS ibling;
                }
                }
                var x= document.getEle mentsByTagName( "div")[0];
                nodeTrav(x);[/CODE]

                Comment

                • Dasty
                  Recognized Expert New Member
                  • Nov 2007
                  • 101

                  #9
                  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.

                  Code:
                  for(i=0;i<elem.childNodes.length;i++)
                  change it to:

                  Code:
                  for(var i=0;i<elem.childNodes.length;i++)
                  always check the variable's scope in recursion!

                  Comment

                  • rohitchawla
                    New Member
                    • Jul 2007
                    • 85

                    #10
                    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

                    Comment

                    • Dasty
                      Recognized Expert New Member
                      • Nov 2007
                      • 101

                      #11
                      Originally posted by rohitchawla
                      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

                      [CODE=javascript]function nodeTrav(elem)
                      {
                      while(elem)
                      {
                      if(elem.nodeTyp e==1)
                      {
                      alert("parent node is:"+elem.paren tNode.nodeName+ " current node is:"+elem.nodeN ame);
                      for(i=0;i<elem. childNodes.leng th;i++)
                      nodeTrav(elem.c hildNodes[i]);
                      }
                      elem=elem.nextS ibling;
                      }
                      }
                      [/CODE]

                      INTO:

                      [CODE=javascript]function nodeTrav(elem)
                      {
                      while(elem)
                      {
                      if(elem.nodeTyp e==1)
                      {
                      if (elem.childNode s.length > 0) nodeTrav(elem.f irstChild);
                      }
                      elem=elem.nextS ibling;
                      }
                      }
                      [/CODE]

                      If this is not the case, can you post link to your page that is causing problem?

                      Comment

                      • rohitchawla
                        New Member
                        • Jul 2007
                        • 85

                        #12
                        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.nextS ibling which made me go through all the elements over and over again

                        Comment

                        Working...