[Javascript] problem accessing form field values

Peter Brunone peter at brunone.com
Fri Jan 25 11:41:40 CST 2002


Bill,

    You are correct; you need to use the eval() function, like this:

var st = eval("document.forms[0]." + myField + ".value");

    Effectively, the eval() function takes a string and turns it into a
variable or object reference.

Cheers,

Peter

----- Original Message -----
From: "Bill" <javascript at unlockedideas.com>
To: <javascript at LaTech.edu>
Sent: Friday, January 25, 2002 11:15 AM
Subject: [Javascript] problem accessing form field values


Hi Folks:

I have a little problem that I hope someone can sort out with a quick
glance. I am trying to create a function that will do the following:

* take as input two parameters
* use one of those parameters to identify which form field it should read
the value of
* output a url with three parameters, those being the first two plus the
value from the form field

I get an error when trying to dynamically access the value of the form
field. Do I need to use eval() or something? What does that do anyway?

Here's some code that illustrates what I've got. Give it a try:

<script language=javascript>
<!--

function selector(myCat,myField) {
var st = document.forms[0].myField.value;
document.write ('sel.asp?c=' + myCat+ '&f=' + myField + '&st=' + st)
}

//-->
</script>

<form>
<input type="text" name="emp">
<input type=button value="..." onClick="selector('intl','emp')">
</form>

But instead of giving me a window with the following (and which does come
when hard coding 'var st' with 'document.forms[0].emp.value'):

sel.asp?c=intl&f=emp&st=user_input

I am stuck with this:

Error: 'document.forms.0.myField.value' is null or not an object.

Any help would be great. Thanks.

Bill








More information about the Javascript mailing list