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
<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