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

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

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

Conceptual design