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:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
td { font-size: 200%; }
</style>
<script type="text/javascript">
function removeRows() {
var tableBody = document.getElementById('myTableBody');
for(var i=(tableBody.rows.length-1); i>=0; i--) {
tableBody.removeChild(tableBody.rows[i]);
}
}
function addRows() {
for(var i=0; i<=5; i++) {
var row = document.createElement('tr');
var cell1 = document.createElement('td');
var cell2 = document.createElement('td');
cell1.innerHTML = i + " Left";
cell2.innerHTML = i + " Right";
row.appendChild(cell1);
row.appendChild(cell2);
document.getElementById('myTableBody').appendChild(row);
}
}
</script>
</head>
<body>
<button onclick="removeRows();">Remove Elements</button>
<button onclick="addRows();">Add Elements</button>
<table cellpadding="8" cellspacing="8" border="2">
<tbody id="myTableBody"></tbody>
</table>
</body>
</html>
Should work cross-browser.
Airshow Last edited by Airshow; May 30th, 2009 at 10:59 pm.
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Offline 2,526 posts
since Apr 2009