945,064 Members | Top Members by Rank

Ad:
Mar 11th, 2008
0

Delete all rows from table in javascript

Expand Post »
for(var i = document.getElementById("tableId").rows.length; i > 0;i--)
{
document.getElementById("tableId").deleteRow(i -1);
}

I had to write this because it took me an hour to find out the error about deleting rows from an html table by javascript.
If you do this it generates an error :

for(var i = 0; i <document.getElementById("tableId").rows.length; i++)
{
document.getElementById("tableId").deleteRow(i -1);
}

The code above wont delete all the rows because "i" is not set back to 0 although the table rows' index will be set again beginning from zero after any row is deleted from the table.
Similar Threads
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Mar 11th, 2008
0

Re: Delete all rows from table in javascript

The easiest way to delete all rows from a table is this
javascript Syntax (Toggle Plain Text)
  1. var Parent = document.getElementById(tableID);
  2. while(Parent.hasChildNodes())
  3. {
  4. Parent.removeChild(Parent.firstChild);
  5. }
Note that when using removeChild that if it removes the last element it will also delete the parent node. I guess it assumes that if you have no child nodes then you don't need the parent node.

Also, don't make a habit of making function calls inside the loop, it is costly.
javascript Syntax (Toggle Plain Text)
  1. var Parent = document.getElementById(tableID);
  2. while(Parent.hasChildNodes())
Is MUCH more preferable to
javascript Syntax (Toggle Plain Text)
  1. while(document.getElementById(tableID).hasChildNodes())
Last edited by ShawnCplus; Mar 11th, 2008 at 8:55 pm.
Sponsor
Reputation Points: 526
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Mar 14th, 2008
0

Re: Delete all rows from table in javascript

If you still need it, this will delete all rows from your table, except for your head row.
javascript Syntax (Toggle Plain Text)
  1. var table = document.getElementById("tableid");
  2. //or use : var table = document.all.tableid;
  3.  
  4. for(var i = table.rows.length - 1; i > 0; i--)
  5. {
  6. table.deleteRow(i);
  7. }
Change the red "0" to "-1" if you wish to delete all rows, including the head row if you have one.
Last edited by SheSaidImaPregy; Mar 14th, 2008 at 12:01 pm.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Mar 16th, 2008
0

Re: Delete all rows from table in javascript

The deleteRow(index) approach used here is much better than the ad hoc removeChild() approach IMO.

Here is my stab at it (untested)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv"Script-Content-Type" content="text/javascript">
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <meta http-equiv="Expires" content="0"> <!-- disable caching -->
  8. <title>Example</title>
  9. <script type="text/javascript">
  10. function deleteRows(id, toDeleteHeader) {
  11. var obj = document.getElementById(id);
  12. if(!obj && !obj.rows)
  13. return;
  14. if(typeof toDeleteHeader == 'undefined')
  15. toDeleteHeader = false;
  16. var limit = !!toDeleteHeader ? 0 : 1;
  17. var rows = obj.rows;
  18. if(limit > rows.length)
  19. return;
  20. for(; rows.length > limit; ) {
  21. obj.deleteRow(limit);
  22. }
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <table id="tbl" name="tbl" border="1">
  28. <thead>
  29. <tr>
  30. <td>No.</td>
  31. <td>Content</td>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <tr>
  36. <td>1</td>
  37. <td>Something</td>
  38. </tr>
  39. <tr>
  40. <td>1</td>
  41. <td>Something</td>
  42. </tr>
  43. <tr>
  44. <td>1</td>
  45. <td>Something</td>
  46. </tr>
  47. <tr>
  48. <td>1</td>
  49. <td>Something</td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. <br><br>
  54. <a href="#" onclick="deleteRows('tbl', false);">Delete rows</a>
  55. </body>
  56. </html>
Last edited by ~s.o.s~; Mar 16th, 2008 at 7:17 am.
Super Moderator
Featured Poster
Reputation Points: 3242
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,874 posts
since Jun 2006
Nov 25th, 2010
0
Re: Delete all rows from table in javascript
i think the fastest and most efficient way is like this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.getElementById("tableId").innerHTML = "";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bertyhell is offline Offline
17 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Google Maps - How do I make a connections map like this?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How can we delete array in javascript?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC