Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Odd -- document.getElementById doesn't seem to work in this context

Reply
Thread Tools

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

 
 
weston
Guest
Posts: n/a
 
      07-19-2005
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-net...ll.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

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-19-2005
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-net...ll.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
 
Reply With Quote
 
 
 
 
weston
Guest
Posts: n/a
 
      07-19-2005
Thank you both! It's obvious now, but I would have taken altogether too
long to think of that myself. Appreciate the help.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd behavior of object equality/identity in the context of relativevs fully qualified imports Nathan Rice Python 0 12-15-2011 02:34 PM
Odd error I can't seem to recreate Noah Roberts C++ 13 11-05-2011 09:35 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM



Advertisments