[Resolved] Form Object arrays in Javascript
I have a bunch of checkboxes on a page. they are added to the form using server script. the final source looks like
Code:
<input id="Documents2_Repeater1__ctl3_0" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
<input id="Documents2_Repeater1__ctl3_1" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
<input id="Documents2_Repeater1__ctl3_2" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" />
Is there any way using client Javascript to loop through these checkboxes and add the value field to an array list? I do not know how many checkboxes there will be. It depends on the database.
Re: Form Object arrays in Javascript
You firstly need to give them all identical names, i.e:
Code:
<input id="Documents2_Repeater1__ctl3_0" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
<input id="Documents2_Repeater1__ctl3_1" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
<input id="Documents2_Repeater1__ctl3_2" type="checkbox" value="3996a1f6-7e71-4dbe-b104-6f1abb7a0dfa" name="test"/>
Then you can access it as an array of the parent form and loop through it like you would any other array:
HTML Code:
<script type="text/javascript">
<!--
var theForm = document.formname;
var count = theForm.test.length;
for (var i = 0; i < count; i++)
{
alert(theForm.test[i].value);
}
//-->
Re: Form Object arrays in Javascript
Or, you don't have to bother with the names and use a while loop instead:
HTML Code:
<script type="text/javascript">
<!--
i=0
MyArray = new Array()
while (document.getElementById('Documents2_Repeater1__ctl3_'+i))
{
MyArray[i] = document.getElementById('Documents2_Repeater1__ctl3_'+i).value
i++
}
-->
</script>
[Resolved] Form Object arrays in Javascript