html - javascript get dom element children from all depths -


i have html code this:

<div id="test">     <div id="test1">         <div id="test2">             test         </div>     </div> </div> 

i want take children of element "test", made recursive function:

function getobjectchildren(elem) {     var elemchildren = elem.childnodes;     var result = [];     result.push.apply(elemchildren);     (k = 0; k < elemchildren.length; k++) {         if (elemchildren[k].childnodes.length > 0) {             result.push.apply(getobjectchildren(elemchildren[k]));         }     }     return result; }  var = getobjectchildren(document.getelementbyid("test"); 

the thing is, recursive function enters infinite loop. know why happens , how can solve problem?

i fixed problem:

function getobjectchildren(elem) {     var elemchildren = elem.childnodes;     var result = [];     (var k = 0; k < elemchildren.length; k++) {         result.push(elemchildren[k]);         if (elemchildren[k].childnodes.length > 0) {             var tmp = getobjectchildren(elemchildren[k]);             (var = 0; < tmp.length; i++) {                 result.push(tmp[i]);             }         }     }     return result; } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -