PDA

View Full Version : How to remove all childNodes?



schoppet
9 Mar 2007, 4:38 AM
Hi,

I just tried to remove all childNodes from a TreeNode-Element, but I don't get it to work.

I have two functions:



fill : function(){
CAI.tree.getRootNode().appendChild(new Ext.tree.TreeNode({text:'Test'}));
},
del : function(){
var temp = CAI.tree.getRootNode().childNodes;
for(var i = 0, len = temp.length; i < len; i++) {
CAI.tree.getRootNode().removeChild(temp[i]);
}
}


The "fill"-function works as expected, but the "del"-function only removes some childNodes and then exits with the error-message "node has no properties [node.ui.remove()]"

After some more clicks on the "del"-function everything is removed, but I want it within one call.

Any hints?

Regards

Joerg

Animal
9 Mar 2007, 4:58 AM
Yes! if you remove the nodes from the first upwards towards a pre-measured length, then pretty soon, you're going to run off the end!

schoppet
9 Mar 2007, 5:03 AM
OK,

but this code has the same effect:



var temp = CAI.tree.getRootNode().childNodes;
for(cs in temp) {
CAI.tree.getRootNode().removechild(temp[cs]);
}

Animal
9 Mar 2007, 5:20 AM
Try



var node = /// The node you want to remove all children from
while (node.hasChildNodes()) {
node.removeChild(node.item(0));
}

schoppet
9 Mar 2007, 5:22 AM
Thank you very much for this fast help.

It works as expected.