PDA

View Full Version : getElementById problem in IE



jonathanweber
11-01-2006, 08:32 PM
I was using a getElementById and kept getting an error in IE. (Firefox was fine.) So, I stripped out all the guts of the code and just tried this:


ar5000 = document.getElementById("ar5000");
alert(ar5000.getAttribute("id"));

As expected, Firefox displays an alert box with the id (ar5000). IE, however, gives 'ar5000 is null or not an object'.

It seems so simple and straightforward. What's wrong?

Twey
11-01-2006, 08:42 PM
Are you sure the element exists and the page has loaded fully? Execute this code in an onload event and see if it works.

jonathanweber
11-01-2006, 08:49 PM
The element does exist. I tried calling the function in an onload, and the same error still occurs.

Twey
11-01-2006, 08:51 PM
In that case, I will need to see the page.

jonathanweber
11-01-2006, 08:56 PM
Hmm, I was able to further isolate the problem: it apparently only occurs only when I call the id of an <object> tag. I guess it's probably something to do with awkward support of <object> in IE? maybe?

In any case, I can work around the problem by restructuring the tags slightly. Thanks a lot for your help, though. Mostly I just needed to hear that I wasn't making some really stupid mistake in expecting that to work correctly.

tech_support
11-02-2006, 06:08 AM
Try this:



<script type="text/javascript">
window.onload=function(){
alert(document.getElementById("ar5000").id);
}
</script>

penguin
11-19-2006, 02:07 PM
I have ran into this problem a lot. The reason I found is that the CSS is not being initiated first. Just initiate your ID's before you call your getElementById. This should do the trick.

ItsMeOnly
11-19-2006, 10:21 PM
try

var ar5000 = document.getElementById("ar5000");
and probably all will be well, IE hates changing type of pointer on the fly: here, id of an element to variable name- stupid but true