...

View Full Version : Need help accessing form elements using variables.



SheinTao
07-10-2006, 05:05 PM
So i thought i was being clever. I put together some code that would create a HTML form relevant to the user according to previously entered information. Elements in the form are created as needed and named using an array variable. However, i can't figure out how to access the form's information. Below is a very basic example of what i mean:


<html>
<head>
<title>test</title>
<script language="JavaScript">

function MakeForm()
{
for (i = 0; i <= orderCnt-1; i++)
{
document.write("Order " + i + ": <br>")
document.write("<input type='radio' name='" + orders[i] + "' value='apple' checked> Apple<br>")
document.write("<input type='radio' name='" + orders[i] + "' value='Peach'> Peach<br>")
document.write("<input type='radio' name='" + orders[i] + "' value='Pear'> Pear<br><br>")
}
}

function ProcessForm(form)
{
alert("Help! I can't access the form elements by name!")
}

</script>

</head>
<body>
<script language="JavaScript">
orders = new Array()
orders[0] = "Jerry"
orders[1] = "Tom"
orders[2] = "Lisa"

orderCnt = 3
</script>

<form name="test">
<script language="JavaScript">
MakeForm();
</script>
<input type="button" value="submit" onCLick="ProcessForm(this.form);">

</form>

</body>
</html>

The first form element should be named "Jerry". However, trying access a value with something like "document.test.Jerry[0].value" causes an error, as does using the array itself (which would be much more useful for checking the form with a loop) like "document.test.orders[0][0].value".

Can anyone help me understand how I can access form elements which were named using an array variable? Any advice you can offer would be greatly appreciated!

Thanks in advance,
-Scott

glenngv
07-10-2006, 07:03 PM
The radio buttons are named differently so they don't form an array. Each radio button is an independent control.

SheinTao
07-10-2006, 11:05 PM
What do you mean? In this case i have 3 sets of 3 radio buttons. All 3 items in a set are named the same, and the 3 sets are named differently. If you play around with the form you'll see that it acts the way it should, as if i had done it with html like:



<input type="radio" name="Jerry" value="apple" checked> Apple<br>
<input type="radio" name="Jerry" value="peach"> Peach<br>
<input type="radio" name="Jerry" value="pear"> Pear<br>
<input type="radio" name="Tom" value="apple" checked> Apple<br>
<input type="radio" name="Tom" value="peach"> Paech<br>
<input type="radio" name="Tom" value="pear"> Pear<br>
<input type="radio" name="Lisa" value="apple" checked> Apple<br>
<input type="radio" name="Lisa" value="peach"> Peach<br>
<input type="radio" name="Lisa" value="pear"> Pear<br>


... only i can't figure out how to access the values :confused:

Beagle
07-11-2006, 03:04 PM
document.form1.Jerry.value
document.form1.Jerry.item(0).value

SheinTao
07-11-2006, 03:15 PM
document.form1.Jerry.value
document.form1.Jerry.item(0).value

Did you try those? They don't work for me...

Beagle
07-11-2006, 03:24 PM
document.form1.Jerry.value doesn't work for me

document.form1.Jerry.item(0).value does

SheinTao
07-12-2006, 03:07 PM
document.form1.Jerry.value doesn't work for me

document.form1.Jerry.item(0).value does

Thanks Beagle, but i still can't get it to work. Where am i going wrong?


<html>
<head>
<title>test</title>
<script language="JavaScript">

function MakeForm()
{
for (i = 0; i <= orderCnt-1; i++)
{
document.write("Order " + i + ": <br>")
document.write("<input type='radio' name='" + orders[i] + "' value='apple' checked> Apple<br>")
document.write("<input type='radio' name='" + orders[i] + "' value='Peach'> Peach<br>")
document.write("<input type='radio' name='" + orders[i] + "' value='Pear'> Pear<br><br>")
}
}


function ProcessForm(form)
{
alert(document.form1.Jerry.item(0).value)
}

</script>

</head>
<body>
<script language="JavaScript">
orders = new Array()
orders[0] = "Jerry"
orders[1] = "Tom"
orders[2] = "Lisa"

orderCnt = 3
</script>

<form name="test">
<script language="JavaScript">
MakeForm();
</script>
<input type="button" value="submit" onCLick="ProcessForm(this.form);">

</form>


</body>
</html>

Beagle
07-12-2006, 03:13 PM
your form is named test, not form1

document.test.Jerry.item(0).value

SheinTao
07-12-2006, 04:15 PM
ahhh, ok... that did it. I tried that before posting, but it must have been the [0] instead of the (0) that was causing the error. Thanks for your time!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum