Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    steping into an infinite loop!

    Hi,
    I've got a problem. I've trying to inspect all of the elements that are into a given div named "myDiv" after some minutes of thinking this is what i came up with.
    Code:
    function launch(){
    if(document.getElementById('myDiv')){
    var nodes = sPage.document.getElementById('myDiv).childNodes;
    for (i=0;i<nodes.length-1 ;i++ )
    {
    if (nodes[i].hasChildNodes() && nodes[i].nodeType==1)
    {
    console.innerHTML += '<br />' + nodes[i].nodeName;
    searchForChildren(nodes[i]);
    }else console.innerHTML += '<br />' + nodes[i].nodeName;
    }
    }
    
    function searchForChildren(n){
    var nodes = n.childNodes;
    for (i=0;i<nodes.length-1 ;i++ )
    {
    if (nodes[i].hasChildNodes() && nodes[i].nodeType==1)
    {
    console.innerHTML += '<br />' + nodes[i].nodeName;
    searchForChildren(nodes[i]);
    }else console.innerHTML += '<br />' + nodes[i].nodeName;
    }
    return;
    }
    because searchForChildren() is always a scope of itself so are the variables of each instance that are created reseting i to 0 and nodes to the previews one ending up into an infinite loop. I know there must be something i can do to make this thing work. anyone has any idea?
    Thanks

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    whay not simply a "wild card"?
    Code:
    var allTagEl=document.getElementById('myDiv').getElementsByTagName('*');
    That will be the collection of all the "tag" elements within that div (not only the direct childNodes, but all the tags nested inside, if this is what you need).
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by AluminX View Post
    because searchForChildren() is always a scope of itself so are the variables of each instance that are created reseting i to 0 and nodes to the previews one ending up into an infinite loop. I know there must be something i can do to make this thing work. anyone has any idea?
    Thanks
    Always declare variables with the var keyword.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •