1

I've been searching the interweb a bunch for this one and I'm stumped. The following Javascript works in Chrome, Firefox, and Safari, but for whatever reason it is not working in IE 7+. What I mean by 'not working' is that when trying to add table elements, nothing shows up in the browser / on the screen. I've used IE's built-in Developer tools, and nothing shows up on the de-bugger in the Console.

In a nutshell, all I'm trying to do is create table rows that have a checkbox in the first column, and plain text in the second column. Also, this entire method is being called after an AJAX event.

Here is the code:

tdObjs[0].innerHTML = '<input type="checkbox" name="page" value="' + pageID + '" 
onClick="fooFunction(\'' + pageID + '\', this,
 \'_images/foo/' + pageID + '.png\', \'' + pageID 
+ '\')" checked="yes">';

Where pageID is a variable passed to this function (in this exact example, its an int value of 5). tdObjs is defined by:

var tdObjs = trObj.getElementsByTagName('TD');

And trObj is passed into this function. trObj does show up in the local window of the IE Developer Tools as it is defined in another function like so:

var tableObj = $('##FooTable')[0];
var trObj = document.createElement('tr');
trObj.appendChild(document.createElement('td'));
for (var i=0;i<2;i++)
        trObj.appendChild(document.createElement('td'));
tableObj.appendChild(trObj);
return trObj;

Any ideas? Thank you in advance.

3
  • innerHTML and tables in IE don't go together. Try putting the table into the document before setting the innerHTML property of the cells. Commented Nov 1, 2011 at 6:05
  • 1
    Great.... Oh, IE lol ... Anyway, the table is already added as plain HTML, and prior to this function call I am deleting all rows in that table. So, should I re-set the table (in other words, delete it when I'm deleting the rows) and then re-set it? And then from there add the rows / columns? Commented Nov 1, 2011 at 6:13
  • I don't know everything you are doing, but if you are adding rows directly to the table element, that will fail in IE, you must add them to the tbody element (a tbody is mandatory, though the tags are optional, there is always a tbody in a table even if you omit the tags). If there is only one (i.e. you don't have any tag sfor it) you can get a reference to it as table.tBodies[0]` Commented Nov 1, 2011 at 6:19

1 Answer 1

1

When adding rows to a table in IE, you must add them to the tbody, e.g.

<table id="foo">
  <tr>
    <td>cell 0
    <td>cell 1
</table>

In the above HTML, browsers will add a tbody element between the table and row elements. The tbody is mandatory but tags are optional. So to add rows you must do something like:

var table = document.getElementById('foo');
var tbody = table.tBodies[0];
var row = document.createElement('tr');
var cell = document.createElement('td');

// Give cell some content and add it
cell.appendChild(document.createTextNode('cell 0'));
row.appendChild(cell);

// Make another cell and add it
cell = cell.cloneNode(false);
cell.appendChild(document.createTextNode('cell 1'));
row.appendChild(cell);

tbody.appendChild(row);

It may be more efficient to clone a row and modify the cell contents rather than making new rows and cells. It's up to you.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this didn't work either. What I decided to do was not delete / modify my table rows, and use <span tags inside the cells instead and set the innerHTML of those. Thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.