Log in

View Full Version : Problem with getting the input values into javascript



sowjanya
11-15-2007, 07:56 PM
Hi All,

I have a form with the input names as an array, i.e. the names of the form text boxes are input[0],input[1],input[2]....

When I use the following i can get a single value

form.elements['input[1]'].value

I want all the values into the java script. I tried the code here and is not working. can you please tell me why?

for(var i=0;i<=val1;i++)
{
val2='input['+i+']';alert(val2);
alert(form.elements[val2].value)
}


I have to validate the user not entering the values into input[1] if input[0] is null and so on, but how do I get the all the values into the javascript. Please help me out.

Thanks,
Sowjanya.

boogyman
11-15-2007, 08:39 PM
for(var i=0; i<=document.frm.input.length; i++)
{
var val[i]= document.frm.input[i].value;
alert(val[i]);
}


where



<form name="frm">
<label>
Field 1<input type="text" name="input[]" value="">
</label>
<label>
Field 2<input type="text" name="input[]" value="">
</label>
<label>
Field <input type="text" name="input[]" value="">
</label>
</form>