Yet Another Javascript Incompatibility

This one’s only minor but got me for a bit a while back…

If you’re trying to add rows to a table you’d naturally do something like this:

var tbl = document.getElementById("myTable");
var row = document.createElement("TR");
var cell = document.createElement("TD");
row.appendChild(cell);
tbl.appendChild(row);

<table id="myTable">
</table>

That will work fine in FireFox but not in IE. The reason is simple: IE automatically adds a TBODY element to your table and expects rows to be added to that instead. So the solution is simple. Change your HTML to:

<table>
    <tbody id="myTable">
    </tbody>
</table>

Then both FF and IE work fine. Maybe I was just being a bit slow when I had that problem.

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s