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
Post a Comment