akram akram - 1 year ago 227
HTML Question

Delete all rows in an HTML table

How can I delete all rows of an HTML table except the

's using Javascript, and without looping through all the rows in the table? I have a very huge table and I don't want to freeze the UI while I'm looping through the rows to delete them

Answer Source

Keep the <th> row in a <thead> and the other rows in a <tbody> then replace the <tbody> with a new, empty one.


var new_tbody = document.createElement('tbody');
old_tbody.parentNode.replaceChild(new_tbody, old_tbody)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download