Posted By

rolandog on 07/09/06


Tagged

node delete DOM iteration everything twin functions


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

luman


Iterative Node Deletion


 / 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.

  1. function killsChildNodes(an_element) {
  2. while (an_element.hasChildNodes()) {
  3. if (!an_element.firstChild.hasChildNodes()) {
  4. var k = an_element.firstChild;
  5. an_element.removeChild(k);
  6. } else {
  7. killsChildNodes2(an_element.firstChild);
  8. }
  9. }
  10. }
  11. function killsChildNodes2(another_element) {
  12. while (another_element.hasChildNodes()) {
  13. if (!another_element.firstChild.hasChildNodes()) {
  14. var k2 = another_element.firstChild;
  15. another_element.removeChild(k2);
  16. } else {
  17. killsChildNodes(another_element.firstChild);
  18. }
  19. }
  20. }
  21. function killAllChildNodesFrom(bob) {
  22. if(document.getElementById(bob).hasChildNodes()) {
  23. killsChildNodes(document.getElementById(bob));
  24. }
  25. }
  26. function killFirstChildNodeFrom(bob) {
  27. if(document.getElementById(bob).hasChildNodes()) {
  28. killsChildNodes(document.getElementById(bob).firstChild);
  29. document.getElementById(bob).removeChild(document.getElementById(bob).firstChild);
  30. }
  31. }
  32. function killLastChildNodeFrom(bob) {
  33. if(document.getElementById(bob).hasChildNodes()) {
  34. killsChildNodes(document.getElementById(bob).lastChild);
  35. document.getElementById(bob).removeChild(document.getElementById(bob).lastChild);
  36. }
  37. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: nareman on April 3, 2007

Nareman want to make love with LediLexa.

Posted By: biddyb1 on September 25, 2010

Interesting.

You need to login to post a comment.