can't reference the input element properly

E

emekadavid

What is the source of the error for this code please: I don't know why
the
var textBox refuses to reference the input element with name

as where.

the html code is

<form name="which" action="">

<input type="text" size="60" name="where">

</form>

the accompanying javascript code is:

<script type="text/javascript" language="JavaScript"><!-- Begin

var isReady= false;

var textBox = document.which.where.value;

textBox = " ";

function showAddress(What){

//if the isReady variable is true

if (isReady){

//change the value of the textbox to the argument returned

textBox = What;

//give the input textbox focus and select it

document.which.where.focus();

document.which.where.select();

}

//else if isReady state is false, throw alert

else {

alert("This page is not fully loaded yet." + "\n" +

"Please wait for the page to finish laoding.");

}

}

//-->

</script>
 
E

Erwin Moller

emekadavid schreef:
What is the source of the error for this code please: I don't know why
the
var textBox refuses to reference the input element with name

as where.

the html code is

<form name="which" action="">

<input type="text" size="60" name="where">

</form>

the accompanying javascript code is:

<script type="text/javascript" language="JavaScript"><!-- Begin

<script type="text/javascript">
will do just fine. ;-)

Don't use the language, and also not the ancient <!--

var isReady= false;

var textBox = document.which.where.value;

That is wrong.
It should be:
var textBox = document.forms.which.where.value;

[didn't check the rest of the code]

Regards,
Erwin Moller



--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
T

Thomas 'PointedEars' Lahn

emekadavid said:
What is the source of the error for this code please: I don't know why
the var textBox refuses to reference the input element with name
as where.

the html code is

<form name="which" action="">

<input type="text" size="60" name="where">

</form>

The form needs a submit button for it to be submitted by all browsers.
the accompanying javascript code is:

<script type="text/javascript" language="JavaScript"><!-- Begin

Forget about the `language' attribute and unnecessary, error-prone pseudo-
comments.

var isReady= false;

Unnecessary, see below.
var textBox = document.which.where.value;

var textBox = document.forms["which"].elements["where"].value;
textBox = " ";

You forgot to declare this identifier as a variable. And why the space?
function showAddress(What){

`What' does not refer to a constructor, so use

function showAddress(what)
{

but you probably want to use a better name for the argument.
//if the isReady variable is true

if (isReady){

This does not make any sense. If you call the function when the `load'
event occurs, as in

<body onload="showAddress(...)">

you can be sure that the document is ready enough.
//change the value of the textbox to the argument returned

textBox = What;

Yet another undeclared identifier, becoming either a property of an object
in the scope chain or causing a runtime error.

Why not name the function argument `textBox' in the first place?

You are not using its value anyway.
//give the input textbox focus and select it

document.which.where.focus();

document.which.where.select();

var o = document.forms["which"].elements["where"];
o.focus();
o.select();

but if you declared

var textBox = document.forms["which"].elements["where"];

in the first place, you could write

... textBox.value ...

textBox.focus();
textBox.select();
}

//else if isReady state is false, throw alert

else {

alert("This page is not fully loaded yet." + "\n" +

"Please wait for the page to finish laoding.");

}

Forget about this nonsense, see above.

Remove that, see above.
</script>

If these suggestions do not help, you may need to reverse the order of
`form' and `script' element. The script code cannot access objects that
would represent markup elements that have not yet been parsed.

alt.comp.lang.javascript does not exist anymore; with Google Groups, you are
using a newsgroup archive.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
emekadavid said:
var textBox = document.which.where.value;

var textBox = document.forms["which"].elements["where"].value;
textBox = " ";

You forgot to declare this identifier as a variable. And why the space?

Ah, sorry, you did not forget. But overwriting the initialization value at
this point does not make any sense either.

That is formally OK, then, although I do not know what you are trying to
accomplish here as ...
You are not using its value anyway.

PointedEars
 
G

GTalbot

What is the source of the error for this code please: I don't know why
the
var textBox refuses to reference the input element with name

as where.

the html code is

<form name="which" action="">

<input type="text" size="60" name="where">

Right here, you can improve your code in 2 ways. Wrapping that input
in a <p> (best forward-compliance: required when using strict DTD) and
giving a better descriptive name to that input. So, e.g.:

<form name="subscribe" action="">

<p><label>City: <input type="text" size="60" name="city"></label></p>

<p><input type="submit"> Submit your form</p>

the accompanying javascript code is:

<script type="text/javascript" language="JavaScript"><!-- Begin

Like everyone said, remove
language="JavaScript"
and remove
<!-- Begin

var isReady= false;

var textBox = document.which.where.value;

The document must be fully loaded and the user must have already
filled in the form control to do this. textBox in your code is a
global variable... not just any variable.
textBox = " ";

If you are going to declare a variable, then best is to always give it
a meaningful name, intuitive name, self-explanatory name. It helps in
all sorts of ways and in the long term to adopt such coding habit.

Here, I would choose

var textBoxValue = document.which.where.value;

or you could go with

var textBox = document.which.where;

As others replied, best web standards form is

var textBox = document.forms["which"].elements["where"];

and this issue was covered by the comp.lang.javascript faq:


8.1 How do I get the value of a form control?
http://jibbering.com/faq/#formControlAccess

and by

Accessing Elements with the W3C DOM
https://developer.mozilla.org/en/Us...e_W3C_DOM#Accessing_Elements_with_the_W3C_DOM

and also by Matt Kruse's Javascript best practices
http://www.javascripttoolbox.com/bestpractices/#forms

function showAddress(What){

//if the isReady variable is true

if (isReady){


You set isReady to false; how will it be updated? Your code excerpt
does not indicate this...
//change the value of the textbox to the argument returned

textBox = What;

//give the input textbox focus and select it

document.which.where.focus();

document.which.where.select();

focus() and select() are most likely part of a form validation process
where the user must re-edit such form control. So, here, you would
usually have an alert or some kind of output saying: "Your entry is
incorrect. Please review that entry." or something like that ... but
your code does not indicate any of this.
}

//else if isReady state is false, throw alert

else {

alert("This page is not fully loaded yet." + "\n" +

"Please wait for the page to finish laoding.");

}
}

//-->

Like everyone else replied, remove
//-->
as this is no longer required.

regards, Gérard
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top