Forum Moderators: open

Message Too Old, No Replies

innerHTML not working in IE

assigning innerHTML causes an error

         

max4

11:07 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



So I'm writing a script with the intention of adding a column to an empty row in a table. And I'd like to do it with innerHTML. However, IE7 doesn't seem to like the assignment of innerHTML.

i.e.:

<tr id="t1"></tr>

<a href... onclick="updateRow();"...

<script language="javascript">
<!--
function updateRow() {
document.getElementById("t1").innerHTML="<td>stuff</td>";
}
-->
</script>

Now, I've definitely narrowed it down to the assignment of innerHTML. I've even checked to see that I can read from innerHTML, and that works fine. Any suggestions?

rocknbil

3:20 am on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype on one line -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
function updateRow() {
document.getElementById("t1").innerHTML="stuff";
return false;
}
</script>
</head>
<body>
<table>
<tr><td><a href="#" onclick="return updateRow();">Test</a></td></tr>
<tr><td id="t1"></td></tr>
</table>
</body>
</html>

Which works, but reveals IE has a problems with creating a TD using innerHTML. You want to create columns. So I think you're going to need to explore document.createElement. Something like this should get you started:

row = document.getElementById('t1');
cell = document.createElement('td');
row.appendChild(cell);
cell.innerHTML = 'stuff in row 1 cell 1';

torbol

12:34 pm on Jun 24, 2008 (gmt 0)

10+ Year Member



I'm recommending this solution: [developer.mozilla.org...]
There is DOM table api. It will better than innerHTML