Please Log In to save to favorites
TAGs
empty, delete, nodes, children


Versions
Empty a DOM container, or delete all child nodes of an object
by craigae
Sometimes you need to clear an element of all contents, but there is no official 'deleteAllChildren' or 'empty' function to get an empty element. For example, if you're using Javascript to manipulate the page contents, you may need to clear a DIV safely of all elements to repopulate.

In the example, the ID of the object being cleared is PARENTOBJECT.
      
    1.   var emptyThis = document.getElementById('PARENTOBJECT');
    2.   
    3.   while(emptyThis.childNodes.length)
    4.   {emptyThis.removeChild(emptyThis .firstChild);}
Comments