Delete all children nodes

To remove all children nodes (from childNodes list) use:

while(listNode.firstChild) { // delete all nodes
       listNode.removeChild(listNode.firstChild);
}

Of course innerHTML will work too:

listNode.innerHTML = '';

Comments

  1. Thanx, you are a lifesaver!
    Used your code to remove all children except the firs one, consider the following example:

    var prvyoption=document.getElementById('searchinfo');
    while (elm.searchsel.firstChild)
    {
    elm.searchsel.removeChild(elm.searchsel.firstChild);
    }
    elm.searchsel.options[0]=prvyoption;

    simple, isn't it? :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Conceptual design

How do I use InVision

Using Trello and Confluence to manage UX design project. Part 1