I am a beginner at javascript. I want to use javascript to browse all the elements in the dom and print its name, I wrote the following:
function getNumber(parent){
var entiredoc = parent;
var docnodes = entiredoc.childNodes;
return docnodes.length;
}
function browAllDom(parent){
if(parent!=null){
for(i = 0; i < getNumber(parent); i++){
alert(parent.nodeName);
return browAllDom(parent.childNodes[i]);
}
}
}
when I debug, it browses the leaf in the tree dom and exits. I think it has to browse all in the for loop.
Where's the problem? And how can I you fix it?
return browAllDom(parent.childNodes[i]);... means that the loop will run at most one time ... return returns out of enclosing functionparent.getElementsByTagName('*'), that returns a flattened object of all elements inparent.