943,902 Members | Top Members by Rank

Ad:
May 30th, 2009
0

Completely delete a table's elements

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007
May 30th, 2009
0

Re: Completely delete a table's elements

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
Reputation Points: 14
Solved Threads: 22
Junior Poster
JugglerDrummer is offline Offline
138 posts
since Apr 2009
May 30th, 2009
0

Re: Completely delete a table's elements

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>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
May 30th, 2009
1

Re: Completely delete a table's elements

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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
May 30th, 2009
0

Re: Completely delete a table's elements

Click to Expand / Collapse  Quote originally posted by Airshow ...
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adaykin is offline Offline
73 posts
since Feb 2007

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: javascript function issue
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Positioning Ajax Window in Parent Window





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


Follow us on Twitter


© 2011 DaniWeb® LLC