vijayjoshi.org - php | javascript | ajax | and all things web

Internet Explorer innerHTML problem while adding rows in table with javascript

If you are a web developer and work with js/AJAX, surely you would have faced a situation like this.
I had a table in a page. What i had to do was create rows in javascript and set the innerhtml of table to the rows created. Something like this-

var tableContents = "";
for(var i=0;i<;5;i++)
{
	tableContents+= "<tr>";
	tableContents+= "<td>";
	tableContents+= "cell content";
	tableContents+= "</td>";
	tableContents+= "</tr>";
}
document.getElementById("myTable").innerHTML = tableContents;

Of course assuming myTable is the table element available in the page.

I prefer innerHTML over DOM functions as they are faster on all browsers and platforms.

Anyway, I checked above code and found that it worked perfectly in all browsers (firefox, opera …) except IE (http://www.schneier.com/blog/archives/2005/12/internet_explor.html).
No error and nothing, my table was still empty.

Well, after many undsuccessfull attempts with innerHTML,I tried google and came up with this.

http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx
It clearly says-

“The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR. The property has no default value.”

I dont know why they have done this but thats the way IE is.
So keep in mind the next time you have to do something like this.

Related Posts

Posted in : Javascript
Tags: ,

1 Comment to “Internet Explorer innerHTML problem while adding rows in table with javascript”

Add Comments (+)

  1. Mohan says:

    thanks for this helping article.
    This problem really puzzles. You’ve done very good analysis. Keep it up Man!

Trackbacks/Pingbacks

  1. innerhtml of a select box | vijayjoshi.org
  2. IE6, cross browser | vijayjoshi.org
  3. Filling a select box using javascript and jQuery | vijayjoshi.org

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

57,054 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>