Javascript: accessing a form array field
Hi,
I have a form field as follows:
<input type="hidden" name="name1" value="value1" />
<input type="hidden" name="name1" value="value2" />
<input type="hidden" name="name1" value="value3" />
When I submit the request I get the values for "name1" parameter as an array. Before submitting I want to manipulate the values in Javascript. How can I access a particular index value, say name1[0] or name1[1] thru javascript.
Your help will be appreciated.
Thank,
Kiran.
[762 byte] By [
talk2keya] at [2007-11-27 9:53:54]

# 1
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function testF() {
alert(document.myForm.name1[0].value);
alert(document.myForm.name1[1].value);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="myForm">
<input type="hidden" name="name1" value="10">
<input type="hidden" name="name1" value="20">
<input type="button" onclick="testF()" value="click me">
</form>
</BODY>
</HTML>
# 2
Thanks you very much den it is working. But a quick question. why it did not work when I used the following.
var obj = document.getElementById("name1");
obj[0].value = "abc"
obj[1].value = "xyz"
Thanks once again.
# 3
getElementById looks for the id attribute on an object, like <input type="text" id="myID" .....>. this is different than the name attribute we wrote about earlier.
however, IDs should be unique across a single page, you should not use the same ID more than once. I don't know if that approach (using getElementByID() ) will even work and if it does it could be very buggy. In other words, I do not recommend it.
Try to name all your fields uniquely to begin with, but failing that you should at least give them all the same name, not the same ID.