How to empty a node

L

laredotornado

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
 
M

Martin Honnen

laredotornado said:
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.getElementById('thebody');
while (el.hasChildNodes())
{
el.removeChild(el.lastChild);
}

or
el.innerHTML = '';
 
J

Jorge

   var el = document.getElementById('thebody');
   while (el.hasChildNodes())
   {
     el.removeChild(el.lastChild);
   }

or

var a, e= document.getElementById('thebody');
while (a= e.firstChild) {
e.removeChild(a);
}
 
S

SAM

Jorge a écrit :
or

var a, e= document.getElementById('thebody');
while (a= e.firstChild) {
e.removeChild(a);
}

or :

var rws = [];
function emptyTbody(){
var T = document.getElementById('thebody');
R = T.rows;
rws.length = 0;
while(R.length > 0) rws.push(T.removeChild(R[0]));
}
function fillBackTbody(){
var T = document.getElementById('thebody');
R = T.rows
k = 0;
n = rws.length;
if(n > 0) while(R.length < n) { T.appendChild(rws[k]); k++; }
}
 
R

RobG

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.getElementById('theBody');
el.parentNode.replaceChild(el.cloneNode(false), el);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top