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

Data visualization for Roadmap Microsite (Angular and D3.js)

Conceptual design

Four tools for prototyping: Sketch, Figma, Principle, and FramerX