RE: Appending to table cells
- From: Old Pedant <OldPedant@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 25 Aug 2008 19:39:00 -0700
"SharkD" wrote:
The following page works in Firefox but not in IE7:
http://www.geocities.com/mikehorvath.geo/html_table_cell_append_test.html
Oh! I remember now! MSIE doesn't allow you to create table elements using
createElement! You have to use insertRow and insertCell!
Thus:
<html>
<head>
<style>
table { border: solid black 1px; }
th { background-color: pink; }
</style>
<script>
function dothis()
{
var cell = document.getElementById('th1');
var newTable = document.createElement('table');
var newRow = newTable.insertRow( );
var newCell1 = newRow.insertCell( );
var newCell2 = newRow.insertCell( );
var newText = document.createTextNode('trumpet')
while (cell.hasChildNodes())
newCell1.appendChild(cell.removeChild(cell.firstChild));
newCell2.appendChild(newText);
cell.appendChild(newTable);
document.getElementById("show").innerText =
document.getElementById("tbl").innerHTML;
}
</script>
</head>
<body>
<input type="button" value="click" onclick="dothis()"/>
<table id="tbl">
<tr>
<th id="th1">header</th>
</tr>
</table>
<hr>
<div id="show"></div>
</body>
</html>
Of course, *THAT* code bombs out on Firefox. Isn't browser compatibility fun?
.
- Follow-Ups:
- Re: Appending to table cells
- From: dhtml
- Re: Appending to table cells
- From: mikh2161
- Re: Appending to table cells
- References:
- Appending to table cells
- From: SharkD
- Appending to table cells
- Prev by Date: RE: Appending to table cells
- Next by Date: Re: Appending to table cells
- Previous by thread: RE: Appending to table cells
- Next by thread: Re: Appending to table cells
- Index(es):