/ Published in: JavaScript
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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); } }