View Full Version : createElement IE issue

06-18-2010, 07:29 PM
I have a problem that I have been told to fix. The code that is there work perfectly in Firefox, but throws errors in IE and will not work.

var newtbody = document.createElement("tbody");
newtbody.id = "ROW"+newkey;
document.getElementById(trElement).parentNode.insertBefore(newtbody,document.getElementById(trElemen t));
newtbody.innerHTML = gcp;

gcp has the HTML for inside the tbody tag. The error I get in IE is the "Unknown runtime error". I have tried some of the "solutions" that I have seen online but none have worked. Can anyone help?

Old Pedant
06-19-2010, 12:53 AM
Ummm...you already *HAVE* a <tbody> (automatically generated) if your table has a <tr> row in it [at least with IE].

Assuming your document.getElementById(trElement) is indeed getting a reference to a <tr>, then it's no wonder the code barfs. The parentNode of a <tr> *IS* the <tbody> [again, at least in IE], so you are trying to insert a <tbody> *INTO* the existing <tbody>!!!

*possibly* the way to solve this is to check to see if the tbody already exists and, if so, use it instead of trying to create a new one.

Can you start from the <table>, instead of from the existing <tr> row??

var tbl = document.getElementById(idOfTheTable);
var tbody = tbl.getElementsByTagName("tbody");
if ( tbody == null )
var firstRow = tbl.getElementsByTagName("tr")[0];
var tbody = document.createElement("tbody");
tbl.insertBefore( tbody, firstRow );
tbody.innerHTML = gcp;


UTTERLY untested. Just kind of thinking out loud.

Old Pedant
06-19-2010, 01:00 AM
Yeah, in MSIE *and* FF, you automatically get a <tbody> when you create a <table>.

And, as I said, the parentNode of any <tr> is the <tbody>, not the <table>.

So I dunno why your code works in FF, either. Doesn't seem reasonable to insert a tbody into a tbody.

Test code:

function show( )
var tbl = document.getElementById("TheTable");
var tbs = tbl.getElementsByTagName("tbody");
if ( tbs != null && tbs.length > 0 )
alert( "TheTable has a <tbody> with contents:\n" + tbs[0].innerHTML);
} else {
alert( "TheTable does not have a <tbody>");
var tr = document.getElementById("row2");
alert( "The parentNode of row2 is a " + tr.parentNode.tagName
+ " that contains:\n" + tr.parentNode.innerHTML);
<body onload="show()">
<table id="TheTable" border=1 cellpadding=10>
<td>cell 1</td><td>cell 2</td>
<tr id="row2">
<td>cell 1</td><td>cell 2</td>

Try it in both FF and MSIE. And any other browser.

06-21-2010, 04:14 PM
The code for all of this is very bad. My boss if the one who wrote it and now I have to fix it. The trElement is not a tr tag as one might think. It is a tbody tag. Each row is encased in a tbody tag. The new tbody is being created and added in IE. The new html is just not being attributed to it. That is where my problem lies.

Old Pedant
06-22-2010, 02:38 AM
Well, if the innerHTML you are trying to add to the tbody includes <TR> or <TD> elements, then you can't do it that way. MSIE insists that you add them to <TBODY>s using DOM methods, not by just writing HTML.

You might want to read this: