Remove all child elements of a DOM node in JavaScript

前端 未结 30 1266
花落未央
花落未央 2020-11-22 03:28

How would I go about removing all of the child elements of a DOM node in JavaScript?

Say I have the following (ugly) HTML:

&

相关标签:
30条回答
  • 2020-11-22 04:13

    You can remove all child elements from a container like below:

    function emptyDom(selector){
     const elem = document.querySelector(selector);
     if(elem) elem.innerHTML = "";
    }
    

    Now you can call the function and pass the selector like below:

    If element has id = foo

    emptyDom('#foo');
    

    If element has class = foo

    emptyDom('.foo');
    

    if element has tag = <div>

    emptyDom('div')
    
    0 讨论(0)
  • 2020-11-22 04:15

    i saw people doing:

    while (el.firstNode) {
        el.removeChild(el.firstNode);
    }
    

    then someone said using el.lastNode is faster

    however I would think that this is the fastest:

    var children = el.childNodes;
    for (var i=children.length - 1; i>-1; i--) {
        el.removeNode(children[i]);
    }
    

    what do you think?

    ps: this topic was a life saver for me. my firefox addon got rejected cuz i used innerHTML. Its been a habit for a long time. then i foudn this. and i actually noticed a speed difference. on load the innerhtml took awhile to update, however going by addElement its instant!

    0 讨论(0)
  • 2020-11-22 04:17

    In response to DanMan, Maarten and Matt. Cloning a node, to set the text is indeed a viable way in my results.

    // @param {node} node
    // @return {node} empty node
    function removeAllChildrenFromNode (node) {
      var shell;
      // do not copy the contents
      shell = node.cloneNode(false);
    
      if (node.parentNode) {
        node.parentNode.replaceChild(shell, node);
      }
    
      return shell;
    }
    
    // use as such
    var myNode = document.getElementById('foo');
    myNode = removeAllChildrenFromNode( myNode );
    

    Also this works for nodes not in the dom which return null when trying to access the parentNode. In addition, if you need to be safe a node is empty before adding content this is really helpful. Consider the use case underneath.

    // @param {node} node
    // @param {string|html} content
    // @return {node} node with content only
    function refreshContent (node, content) {
      var shell;
      // do not copy the contents
      shell = node.cloneNode(false);
    
      // use innerHTML or you preffered method
      // depending on what you need
      shell.innerHTML( content );
    
      if (node.parentNode) {
        node.parentNode.replaceChild(shell, node);
      }
    
      return shell;
    }
    
    // use as such
    var myNode = document.getElementById('foo');
    myNode = refreshContent( myNode );
    

    I find this method very useful when replacing a string inside an element, if you are not sure what the node will contain, instead of worrying how to clean up the mess, start out fresh.

    0 讨论(0)
  • 2020-11-22 04:17

    Why aren't we following the simplest method here "remove" looped inside while.

    const foo = document.querySelector(".foo");
    while (foo.firstChild) {
      foo.firstChild.remove();     
    }
    
    • Selecting the parent div
    • Using "remove" Method inside a While loop for eliminating First child element , until there is none left.
    0 讨论(0)
  • 2020-11-22 04:17

    Other ways in jQuery

    var foo = $("#foo");
    foo.children().remove();
    //or
    $("*", foo ).remove();
    //or
    foo.html("");
    //or
    foo.empty();
    
    0 讨论(0)
  • 2020-11-22 04:20

    Just saw someone mention this question in another and thought I would add a method I didn't see yet:

    function clear(el) {
        el.parentNode.replaceChild(el.cloneNode(false), el);
    }
    
    var myNode = document.getElementById("foo");
    clear(myNode);
    

    The clear function is taking the element and using the parent node to replace itself with a copy without it's children. Not much performance gain if the element is sparse but when the element has a bunch of nodes the performance gains are realized.

    0 讨论(0)
提交回复
热议问题