|
-
Feb 18th, 2005, 05:48 PM
#1
Thread Starter
Addicted Member
[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.
Last edited by MasterBlaster; Feb 22nd, 2005 at 03:07 PM.
"And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
Frank Zappa
-
Feb 19th, 2005, 03:44 AM
#2
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);
}
//-->
-
Feb 20th, 2005, 07:34 AM
#3
Frenzied Member
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>
Have I helped you? Please Rate my posts. 
-
Feb 22nd, 2005, 03:07 PM
#4
Thread Starter
Addicted Member
[Resolved] Form Object arrays in Javascript
Awesome! Thanks!
"And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
Frank Zappa
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|