tags:

views:

72

answers:

1

I want to replace the <td> tag.

My code looks like this:

var divele = document.createElement("div");
divele.innerHTML = "<td><p>content</p></td>";

var tdele = document.getElementByd("tdid");
tdele.parentNode.replaceChild(divele.firstChild,tdele);

The tdid is replaced with the <p> tag content only not with the <td>.

When I assign the <td> content to the innerHTML of the <div>, the <td> is replaced from that content (as per the block level element).

How do I replace the <td> with the new <td> content using innerHTML?

+1  A: 
var td = document.getElementById('tdid');
td.innerHTML = "<p>content</p>";

simply put the content into the td directly.

replacing td cells can cause weird behaviour and will give you unexpected results if it works at all.

barkmadley
In fact, there are certain cases where doing this kind of replacement can actually cause memory exceptions in IE.
Mark Biek