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

W

weston

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
 
R

RobG

weston said:
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.
 
W

weston

Thank you both! It's obvious now, but I would have taken altogether too
long to think of that myself. Appreciate the help.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top