Return to Snippet

Revision: 300
at July 9, 2006 01:07 by rolandog


Initial Code
function killsChildNodes(an_element) {
	while (an_element.hasChildNodes()) {
		if (!an_element.firstChild.hasChildNodes()) {
			var k = an_element.firstChild;
			an_element.removeChild(k);
		} else {
			killsChildNodes2(an_element.firstChild);
		}
	}
}
function killsChildNodes2(another_element) {
	while (another_element.hasChildNodes()) {
		if (!another_element.firstChild.hasChildNodes()) {
			var k2 = another_element.firstChild;
			another_element.removeChild(k2);
		} else {
			killsChildNodes(another_element.firstChild);
		}
	}
}
function killAllChildNodesFrom(bob) {
	if(document.getElementById(bob).hasChildNodes()) {
		killsChildNodes(document.getElementById(bob));
	}
}
function killFirstChildNodeFrom(bob) {
	if(document.getElementById(bob).hasChildNodes()) {
		killsChildNodes(document.getElementById(bob).firstChild);
		document.getElementById(bob).removeChild(document.getElementById(bob).firstChild);
	}
}
function killLastChildNodeFrom(bob) {
	if(document.getElementById(bob).hasChildNodes()) {
		killsChildNodes(document.getElementById(bob).lastChild);
		document.getElementById(bob).removeChild(document.getElementById(bob).lastChild);
	}
}

Initial URL

                                

Initial Description
This script makes it possible to delete every childNodes, childNodes.childNodes, childNodes.childNodes.childNodes and so on. Includes example functions for deleting all childNodes, firstChild and lastChild from an 'id'. It uses twin functions that call each other until everything is removed.

Initial Title
Iterative Node Deletion

Initial Tags
DOM

Initial Language
JavaScript