473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Jul 23 '05 #1
3 1947
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
Jul 23 '05 #2

As RobG pointed out, is a runtime issue, the element isn't parsed by the
time the code tries to access it, put the .js accessing the object right
before </body>.

Danny

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
Thank you both! It's obvious now, but I would have taken altogether too
long to think of that myself. Appreciate the help.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
2
by: Gary Mayor | last post by:
Hi, I'm back again. Basically i'm trying to draw a box over an image which is turning out to be a nightmare. The problem i'm getting at the moment is that i'm creating a line with <div which works...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
25
by: jullag | last post by:
Hi, does anyone know of any javascript method that does the same job as document.write(), but not necessarily at the end of the document? For instance, insert some text inside an element that...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: ChrisLA | last post by:
Hi; I've seen lots of discussion & disagreement on this issue, so any good explanation would be appreciated. Some people seem to think that "document.GetElementByID("MyName").submit(); should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.