PDA

View Full Version : How to name an array of inputs in JS?


ConfusedOfLife
01-28-2003, 10:55 PM
How can I name my form inputs that I can refer to them as a part of array? Look at this code for example:

<form name="myForm">
<input type="Text" name="test[]" value="a">
<input type="Text" name="test[]" value="b">
<input type="Text" name="test[]" value="c">
<input type="Text" name="test[]" value="d">
<input type="Text" name="test[]" value="e">
<input type="Text" name="test[]" value="f">
<input type="Text" name="test[]" value="g">
</form>

For those who're familiar with PHP, it's clear why I really need such form! But if I want to index one of the inputs in JS, it doesn't recognize it. For example the following code doesn't work:

alert( document.myForm.test[0].value );

Is there anyway to do this? I know about the elements array in JS, but I don't wana use it, since all the inputs are not in a sequential line like the example above.

beetle
01-28-2003, 11:10 PM
Yes! use the elements collection!

document.myForm.elements['test[]'][0].value

ConfusedOfLife
01-28-2003, 11:13 PM
Thanks! I couldn't think of it!