Completely delete a table's elements

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Completely delete a table's elements

 
0
  #1
May 30th, 2009
Hello, I am trying to completely remove everything from a table. I want to delete all of the rows which I am able to do. But when I delete all of the rows and add rows in again there is whitespace at the top of the table and I can't figure out why it is there. Here is my code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. td {
  5. font-size: 200%;
  6. }
  7. </style>
  8.  
  9. <script type="text/javascript">
  10. function removeTables()
  11. {
  12. var table = document.getElementById("myTable");
  13. for(var i = table.rows.length - 1; i >= 0; i--) {
  14. table.deleteRow(i);
  15. }
  16. document.getElementById("myTable").innerHTML = "";
  17. }
  18.  
  19. function addTables()
  20. {
  21. for(var i = 5; i >= 0; i--) {
  22. var row = document.createElement('tr');
  23. var cell1 = document.createElement('td');
  24. var cell2 = document.createElement('td');
  25. cell1.innerHTML = i + " Left";
  26. cell2.innerHTML = i + " Right";
  27. row.appendChild(cell1);
  28. row.appendChild(cell2);
  29. document.getElementById('myTable').appendChild(row);
  30. }
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <table id="myTable" cellpadding="8" cellspacing="8" border="2">
  36.  
  37. </table>
  38. <button onclick="removeTables();">Remove Elements</button>
  39. <button onclick="addTables();">Add Elements</button>
  40. </body>
  41. </html>

After I do an add elements, delete elements, and add elements again, there is whitespace at the top of the table. Repeat the process several times and the whitespace adds up. How do I remove this whitespace?
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 119
Reputation: JugglerDrummer is an unknown quantity at this point 
Solved Threads: 15
JugglerDrummer's Avatar
JugglerDrummer JugglerDrummer is offline Offline
Junior Poster

Re: Completely delete a table's elements

 
0
  #2
May 30th, 2009
using document.getElementById("myTable").innerHTML = ""; will clear everything in the table, no other code required. You can add elements with innerHTML:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var tablehtml="";
  2. var row="<tr>";
  3. for (var i=0; i<number_of_tds; i++)
  4. row+="<td>" + content in td + "</td>";
  5. row += "</tr>";
  6.  
  7. for (var i=0; i<number_of_trs; i++)
  8. tablehtml += row;
  9.  
  10. tableelement.innerHTML=tablehtml;
or use XML methods
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var table=document.createElement("table");
  2. var row=document.createElement("tr");
  3. for (var i=0; i<number_of_tds; i++)
  4. row.appendChild( document.createElement("td") );
  5.  
  6. for (var i=0; i<number_of_trs; i++)
  7. table,appendChild( row ); //you may need to make more rows to append, not sure
92% of all statistics are made up on the spot.

If you found a post helpful, please click the "give XXX reputation" link, to show your appreciation to those who helped you. Thanks! :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Completely delete a table's elements

 
0
  #3
May 30th, 2009
Or you could just create the whole table+elements, instead of having it preloaded in the page.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. td {
  5. font-size: 200%;
  6. }
  7. </style>
  8.  
  9. <script type="text/javascript">
  10. function removeTables()
  11. {
  12. document.getElementById("test").removeChild(document.getElementById("myTable"));
  13. }
  14.  
  15. function addTables()
  16. { cTable = document.createElement("table");
  17. cTable.id = "myTable";
  18. cTable.border = "2";
  19. for ( var i = 0; i < 5; i++ ) {
  20. var row = document.createElement('tr');
  21. var cell1 = document.createElement('td');
  22. var cell2 = document.createElement('td');
  23. cell1.innerHTML = i + " Left";
  24. cell2.innerHTML = i + " Right";
  25. row.appendChild( cell2 );
  26. row.insertBefore( cell1, cell2 );
  27. cTable.appendChild( row );
  28. } document.getElementById("test").appendChild( cTable );
  29. }
  30. </script>
  31. </head>
  32. <body>
  33. <div id="test">
  34.  
  35. </div>
  36. <button onclick="removeTables();">Remove Elements</button>
  37. <button onclick="addTables();">Add Elements</button>
  38.  
  39. </body>
  40. </html>
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 859
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is online now Online
Practically a Posting Shark

Re: Completely delete a table's elements

 
1
  #4
May 30th, 2009
Adaykin,

Original code didn't run in IE6. Would neither add nor delete. Part of the reason is that IE needs thead or tbody elements within a table to make rows addressable. IE also didn't like table.innerHTML = ''; This may also affect other browsers (not tested).

Solution: Remove id from the table and create <tbody id="myTable"></tbody> (in HTML). Add rows as before but delete rows with .removeChild() rather than deleteRow(); or .innerHTML = ""; .

Try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <style type="text/css">
  6. td { font-size: 200%; }
  7. </style>
  8.  
  9. <script type="text/javascript">
  10. function removeRows() {
  11. var tableBody = document.getElementById('myTableBody');
  12. for(var i=(tableBody.rows.length-1); i>=0; i--) {
  13. tableBody.removeChild(tableBody.rows[i]);
  14. }
  15. }
  16.  
  17. function addRows() {
  18. for(var i=0; i<=5; i++) {
  19. var row = document.createElement('tr');
  20. var cell1 = document.createElement('td');
  21. var cell2 = document.createElement('td');
  22. cell1.innerHTML = i + " Left";
  23. cell2.innerHTML = i + " Right";
  24. row.appendChild(cell1);
  25. row.appendChild(cell2);
  26. document.getElementById('myTableBody').appendChild(row);
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <button onclick="removeRows();">Remove Elements</button>
  33. <button onclick="addRows();">Add Elements</button>
  34. <table cellpadding="8" cellspacing="8" border="2">
  35. <tbody id="myTableBody"></tbody>
  36. </table>
  37. </body>
  38. </html>
Should work cross-browser.

Airshow
Last edited by Airshow; May 30th, 2009 at 10:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Completely delete a table's elements

 
0
  #5
May 30th, 2009
Originally Posted by Airshow View Post
Adaykin,

Original code didn't run in IE6. Would neither add nor delete. Part of the reason is that IE needs thead or tbody elements within a table to make rows addressable. IE also didn't like table.innerHTML = ''; This may also affect other browsers (not tested).

Solution: Remove id from the table and create <tbody id="myTable"></tbody> (in HTML). Add rows as before but delete rows with .removeChild() rather than deleteRow(); or .innerHTML = ""; .

Try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <style type="text/css">
  6. td { font-size: 200%; }
  7. </style>
  8.  
  9. <script type="text/javascript">
  10. function removeRows() {
  11. var tableBody = document.getElementById('myTableBody');
  12. for(var i=(tableBody.rows.length-1); i>=0; i--) {
  13. tableBody.removeChild(tableBody.rows[i]);
  14. }
  15. }
  16.  
  17. function addRows() {
  18. for(var i=0; i<=5; i++) {
  19. var row = document.createElement('tr');
  20. var cell1 = document.createElement('td');
  21. var cell2 = document.createElement('td');
  22. cell1.innerHTML = i + " Left";
  23. cell2.innerHTML = i + " Right";
  24. row.appendChild(cell1);
  25. row.appendChild(cell2);
  26. document.getElementById('myTableBody').appendChild(row);
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <button onclick="removeRows();">Remove Elements</button>
  33. <button onclick="addRows();">Add Elements</button>
  34. <table cellpadding="8" cellspacing="8" border="2">
  35. <tbody id="myTableBody"></tbody>
  36. </table>
  37. </body>
  38. </html>
Should work cross-browser.

Airshow

Thanks Airshow, that's what I was looking for!
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC