getElementById return value differences in Firefox/IE

T

timetaco

Relatively new to Javascript, please be gentle...

In Firefox 2, document.getElementById is returning an HTMLDivElement,
in IE 7, it is returning an Object.

For example:

<div id="errorTableDiv">
</div>

....

errorTableDiv = document.getElementById("errorTableDiv");
alert(errorTableDiv);
errorTableDiv.appendChild(someTableNode);

Firefox prints HTMLDivElement and lets me appendChild() later on
IE prints Object and gives an error on the appendChild()

I do not understand why this happens or what the most socially
acceptable way to fix it is.
 
R

RobG

Relatively new to Javascript, please be gentle...

In Firefox 2, document.getElementById is returning an HTMLDivElement,
in IE 7, it is returning an Object.

For example:

<div id="errorTableDiv">
</div>

...

errorTableDiv = document.getElementById("errorTableDiv");
alert(errorTableDiv);
errorTableDiv.appendChild(someTableNode);

Firefox prints HTMLDivElement and lets me appendChild() later on
IE prints Object and gives an error on the appendChild()

That IE and Firefox report differently isn't a problem, that's just a
difference in the browsers. Use:

alert(errorTableDiv.tagName);

to see what you've got. To understand the appendChild issue, you're
going to have to post a minimal example that demonstrates the issue.
 
T

timetaco

Thanks, this pointed me in the right direction. I looked up the
appendChild rules and I was appending to the table node instead of the
tbody itself.

It's working now.
 
R

RobG

Thanks, this pointed me in the right direction. I looked up the
appendChild rules and I was appending to the table node instead of the
tbody itself.

Please don't top post here, reply below trimmed quotes.

I had in inkling you were using tables, but your example was a div so
let it slide. To put rows into tables, use the table's insertRow
method, then you don't care about tBody elements (which will be
inserted into tables by all browsers whether there are tbody tags in
the HTML or not):

<URL: http://developer.mozilla.org/en/doc...ples#Example_8:_Using_the_DOM_Table_Interface
You can also use the insertRow method with a tBody (or other
tableSection element) if you wish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top