Saturday, September 23, 2006

Can't add dynamic HTML content to a table in IE.

I found that adding HTML elements dynamically using JavaScript into a table, doesn't work in IE. Works fine in Gecko based browsers.

var placeHolder = document.getElementById("tableID");
var newElement = document.createElement("tr");
newElement.innerHTML = "Dynamic content";
placeHolder.appendChild(newElement);

This doesn't work as expected in IE.
If u want a workaround for this problem, then you may create a DIV element inside a or .

var placeHolder = document.getElementById("tdOrtrID");
var newElement = document.createElement("div");
newElement.innerHTML = "Dynamic content";
placeHolder.appendChild(newElement);

This works properly, but you have to handle the layout problems since its not a tr element.

No comments: