...

View Full Version : steping into an infinite loop!



AluminX
12-21-2007, 03:54 PM
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.


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

Kor
12-21-2007, 04:03 PM
whay not simply a "wild card"?


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

Arty Effem
12-21-2007, 05:59 PM
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?
ThanksAlways declare variables with the var keyword.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum