PDA

View Full Version : Remove Children with Javascript



Jeff Wheeler
February 18th, 2005, 11:21 PM
Alright, don't ask why, but I've got to do something very strange.

I need to remove all children of the body tag. I'm been trying for hours something like this:


for(i=0;i < document.childNodes[1].childNodes[1].length;i++) {
document.childNodes[1].childNodes[1].removeChild[i]
}

But of course, that's completely wrong. I admit, I'm trying to do a somewhat difficult project with little javascript knowledge, but I will continue to try.

Thanks,
Jeff (how I love when my messages fit exactly inside the box...)

Jeff Wheeler
February 19th, 2005, 09:37 AM
I hate being impatient, but I want to finish this!

λ
February 19th, 2005, 09:40 AM
You need to do something recursive. In true pseudo code:



function removeTree (node) {
if (node has children) {
for every child node {
removeTree (child node);
}
} else {
delete node;
}
}

removeTree (document);


Hit me up on IM if you need help with it :)

Kristopher
February 19th, 2005, 04:01 PM
Does this topic bind me to the Patriot Act?

Jeff Wheeler
February 19th, 2005, 04:02 PM
what?