Hello,
I'm trying to use document.getElementById().innerHTML in a JavaScript to change information in a webpage. On FireFox this works as described in the W3C documentation, however, the same method returns 'Unknown Error' in IE. The JavaScript looks like this:
<script type=text/javascript>
function Change_Info (ID, ROW, VALUE) {
if (document.getElementById){
var ntext = "<td width=4\% bgcolor=#FFFFFF> </td><td width=92\% bgcolor=#FFFFFF colspan=2><font face=Arial size=2 color=#5578C4>" + VALUE + "</font></td><td width=4\% bgcolor=#FFFFFF><center> </center></td>";
document.getElementById( ID + "-" + ROW).innerHTML = ntext;
return false;
}
}
The script is called by a MouseOver event like this:
onmouseover='Change_Info("thetag","1","Some Info");
The script would combine ID with a - and then ROW, which, in this example would be, thetag-1. The exact tag does exist in the html document. Using getElementById with the hardcoded tag name, reveils the same error, and the variable method is the prefered one in this situation.
To questions regarding why full html table information is in ntext, for whatever reason nested ID's fail on *both* FireFox and IE, even though the W3C specification states it should work (obviously both browsers have not fully implimented the W3C specs as persceribed). If someone knows of the way to access and change nested ID's, that works in both FireFox and IE, I'd sure like to know it.
Additionally, as yet I'm only getting this 'Unknown Error' in IE when using innerHTML to *change* the information. Reading works without error.
Can someone point out where my scripting error is so that I can swap text 'messages' on mouseover events.
Thanks.

document.getElementById().innerHTML fails with 'Unknown Error' in IE
Marius_m
The bugs are reported/tracked here:
http://webbugtrack.blogspot.com/2007/12/bug-210-no-innerhtml-support-on-tables.html
Your best bet is to insert the whole table as one .innerHTML call, or use the DOM methods (be weary of attribute naming)
xPropHead
New2Avalon
I tried to discover if I even had the right <div> using...
var container = document.getElementById("total");
alert(container.innerHTML);
It came up empty when it shouldn't. So I gave the <div> a name, "name=blah" and tried to see it.
alert(container.name);
The name the alert gave me was "total"!!!!! So I searched through my code to see what else used the name "total." Sure enough, I found something. What my code had was...
...
<input type=hidden name=total value=0>
...
<div id=total name=blah>some text</div>
Brain-dead IE was taking the first item using EITHER the name or ID assigned "total" rather than the ONLY item with an ID of "total." It took me hours to figure this out.
I changed my <div>'s ID to "dangiedivtotal" and re-ran my alert test. Sure enough, everything worked, including the innerHTML assignment.
So, Rule #1: never use the same name for name= or id= in multiple tags assuming IE is smart enough to know the difference between them --- it isn't. IE's implementation of getElementById looks at **both** the name= and id= references and takes the first one it finds.
niceanimesh
tried
"getElementsByName" (instead of "getElementById")... no luck
tried using
name="myelement" id="myelement" .... no luck
also tried
document.all.getElementById ... no luck
Of course there's no issue in Firefox...., just IE...
barontufsap
The source of the error, though, was simply in what I was putting in the element via innerHTML. eg, don't put <form> or <p> inside a <p> element!
I hope this helps someone. I'm sure I'll run into the error again, though. Apparently it comes up quite a bit, with very little explaination.
monika
jackrackam
someone please help! (it would help if everyone used firefox.....)
pwi11
(it would help if everyone used firefox..)
vessel
this fails:
<form id='test'></form>
<script>document.getElementById('test').innerHTML = 'whatever';</script>
this works:
<span id='test'><form></form></span>
<script>document.getElementById('test').innerHTML = '<form>whatever</form>'; </script>
loki55
Thanks a million!
GuestMan
//http://www.permadi.com/tutorial/jsInnerHTMLDOM/index.html
//hope this helps...
if (document.getElementById)
{
var replace=document.getElementById('abc');
if (replace)
{
if (replace.childNodes[0])
{
replace.childNodes[0].nodeValue=thisHtml;
}
else if (replace.value)
{
replace.value=thisHtml;
}
else //if (replace.innerHTML)
{
replace.innerHTML=thisHtml;
}
}
}
Jack.NET
I no longer need your assistance.