How to empty a node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado

    How to empty a node

    Hi,

    I have a TBODY node with id="thebody". How do I use JS to empty all
    its contents so that the DOM after the call would look like

    <tbody id="thebody"></tbody>

    ? Thanks, - Dave
  • Martin Honnen

    #2
    Re: How to empty a node

    laredotornado wrote:
    I have a TBODY node with id="thebody". How do I use JS to empty all
    its contents so that the DOM after the call would look like
    >
    <tbody id="thebody"></tbody>

    var el = document.getEle mentById('thebo dy');
    while (el.hasChildNod es())
    {
    el.removeChild( el.lastChild);
    }

    or
    el.innerHTML = '';


    --

    Martin Honnen

    Comment

    • Jorge

      #3
      Re: How to empty a node

      On Sep 19, 5:18 pm, Martin Honnen <mahotr...@yaho o.dewrote:
         var el = document.getEle mentById('thebo dy');
         while (el.hasChildNod es())
         {
           el.removeChild( el.lastChild);
         }
      or

      var a, e= document.getEle mentById('thebo dy');
      while (a= e.firstChild) {
      e.removeChild(a );
      }
      or
         el.innerHTML = '';
      >

      Comment

      • SAM

        #4
        Re: How to empty a node

        Jorge a écrit :
        On Sep 19, 5:18 pm, Martin Honnen <mahotr...@yaho o.dewrote:
        >
        > var el = document.getEle mentById('thebo dy');
        > while (el.hasChildNod es())
        > {
        > el.removeChild( el.lastChild);
        > }
        >
        or
        >
        var a, e= document.getEle mentById('thebo dy');
        while (a= e.firstChild) {
        e.removeChild(a );
        }
        >
        >or
        > el.innerHTML = '';
        >>
        or :

        var rws = [];
        function emptyTbody(){
        var T = document.getEle mentById('thebo dy');
        R = T.rows;
        rws.length = 0;
        while(R.length 0) rws.push(T.remo veChild(R[0]));
        }
        function fillBackTbody() {
        var T = document.getEle mentById('thebo dy');
        R = T.rows
        k = 0;
        n = rws.length;
        if(n 0) while(R.length < n) { T.appendChild(r ws[k]); k++; }
        }

        --
        sm

        Comment

        • RobG

          #5
          Re: How to empty a node

          On Sep 20, 1:16 am, laredotornado <laredotorn...@ zipmail.comwrot e:
          Hi,
          >
          I have a TBODY node with id="thebody".  How do I use JS to empty all
          its contents so that the DOM after the call would look like
          >
          <tbody id="thebody"></tbody>
          >
          ?  Thanks, - Dave
          For a large number of rows, it may be faster to replace the tbody with
          a shallow clone of itself:

          var el = document.getEle mentById('theBo dy');
          el.parentNode.r eplaceChild(el. cloneNode(false ), el);


          --
          Rob

          Comment

          Working...