473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Odd -- document.getEle mentById 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.getEle mentById 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="enrollCodeF ield" 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.getEle mentById('enrol lCodeField');
if(x) x.value = ec;
else alert('Can\'t find the input given id="enrollCodeF ield"');

(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.getEle mentById('enrol lCodeField') 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.getEle mentById('enrol lCodeField'), it works as expected.

Any idea what I'm missing here?

Thanks,

Weston

Jul 23 '05 #1
3 1964
weston wrote:
Maybe I just need another pair of eyes or two, but it really seems as
if document.getEle mentById 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="enrollCodeF ield" 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.getEle mentById('enrol lCodeField');
if(x) x.value = ec;
else alert('Can\'t find the input given id="enrollCodeF ield"');

(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.getEle mentById('enrol lCodeField') 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.getEle mentById('enrol lCodeField'), 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. 'enrollCodeFiel d' 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
6847
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 these problems is using document.layers. I have Google'd for examples of how to use the document object specifically with Mozilla, but I cannot find anything that explains why my problems occur. Could anyone here see through the included example...
2
20948
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 when it's not hidden but I need to be able to make it hidden so I can use layers to show all when it finished drawing to make it smoother. This is how some other scripts are doing it that i've seen. So i've got it drawing a line and I need...
4
5485
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) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
3
9276
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 work on FireFox or Netscape. What could be wrong? The function: function setActiveTab(tabNo) {
25
3511
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 has a specific ID tag? thanks a lot JL
136
9460
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
7
2408
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 of the questions, that form is hidden and the next is displayed. Also, as the user answers each question a "count" variable is updated based on their response. I would like the question to show up on the left side and the answer to show up in the...
20
7013
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
2
3140
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 and does work. I and others have experienced that it should & doesn't work. I'll give you a little file that I tested with IE 6, FF 3, Opera & Chrome. Only the form button, document.myname.submit(); and document.forms.submit(); worked. Take a...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.