Results 1 to 8 of 8

Thread: getElementById problem in IE

  1. #1
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getElementById problem in IE

    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:

    Code:
    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?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Are you sure the element exists and the page has loaded fully? Execute this code in an onload event and see if it works.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The element does exist. I tried calling the function in an onload, and the same error still occurs.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In that case, I will need to see the page.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try this:

    Code:
    <script type="text/javascript">
    window.onload=function(){
    alert(document.getElementById("ar5000").id);
    }
    </script>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Nov 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getElementById

    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.

  8. #8
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    try
    Code:
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •