Odd -- document.getElementById doesn't seem to work in this context

Discussion in 'Javascript' started by weston, Jul 19, 2005.

  1. weston

    weston Guest

    Maybe I just need another pair of eyes or two, but it really seems as
    if document.getElementById is choking on me. This page:

    http://www.mortgageplans.com/mbc-network/enroll.html?EC=245

    has an input form field coded like so:

    <input id="enrollCodeField" type="text" name="Code" value="" size="9"/>

    And I've got the following javascript, which tries to get a reference
    to that element and change its value:

    x = document.getElementById('enrollCodeField');
    if(x) x.value = ec;
    else alert('Can\'t find the input given id="enrollCodeField"');

    (The whole script context can be found at
    http://www.mortgageplans.com/javascript/refcode.js )

    The above code always seems to trigger the alert, meaning....
    document.getElementById('enrollCodeField') apparently isn't retrieving
    a reference to the input element id'd as enrollCodeField. I can't see
    any apparent reason for this.

    Oddly enough, if I call the Javascript Shell Bookmarklet
    (http://www.squarefree.com/shell/ ) on that page, and type in
    document.getElementById('enrollCodeField'), it works as expected.

    Any idea what I'm missing here?

    Thanks,

    Weston
    weston, Jul 19, 2005
    #1
    1. Advertising

  2. weston

    RobG Guest

    weston wrote:
    > Maybe I just need another pair of eyes or two, but it really seems as
    > if document.getElementById is choking on me. This page:
    >
    > http://www.mortgageplans.com/mbc-network/enroll.html?EC=245
    >
    > has an input form field coded like so:
    >
    > <input id="enrollCodeField" type="text" name="Code" value="" size="9"/>
    >
    > And I've got the following javascript, which tries to get a reference
    > to that element and change its value:
    >
    > x = document.getElementById('enrollCodeField');
    > if(x) x.value = ec;
    > else alert('Can\'t find the input given id="enrollCodeField"');
    >
    > (The whole script context can be found at
    > http://www.mortgageplans.com/javascript/refcode.js )
    >
    > The above code always seems to trigger the alert, meaning....
    > document.getElementById('enrollCodeField') apparently isn't retrieving
    > a reference to the input element id'd as enrollCodeField. I can't see
    > any apparent reason for this.
    >
    > Oddly enough, if I call the Javascript Shell Bookmarklet
    > (http://www.squarefree.com/shell/ ) on that page, and type in
    > document.getElementById('enrollCodeField'), it works as expected.
    >
    > Any idea what I'm missing here?


    Yes, time.

    Your script is run as the page loads and before the document is
    completed. 'enrollCodeField' doesn't exist when you try to access it.

    Run the script from the body onload or place the script element
    containing refcode.js after all the elements that it tries to access.


    --
    Rob
    RobG, Jul 19, 2005
    #2
    1. Advertising

  3. weston

    weston Guest

    Thank you both! It's obvious now, but I would have taken altogether too
    long to think of that myself. Appreciate the help.
    weston, Jul 19, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    29
    Views:
    1,010
    tshad
    Sep 7, 2007
  2. nick
    Replies:
    2
    Views:
    186
  3. wk
    Replies:
    5
    Views:
    160
    Dietmar Meier
    Apr 22, 2005
  4. Martin
    Replies:
    3
    Views:
    135
  5. Replies:
    7
    Views:
    146
    McKirahan
    Jan 31, 2006
Loading...

Share This Page