Checkboxes and arrays

gilbertsavier's Avatar
Banned
Hi,
I need some code which can give an array some values, depending on which checkboxes which is selected.
I think I will give these checkboxes (lets say 10 checkboxes) some values, and when you have selected the relevant checkboxes, you will have to click a button: "Create array". Then a array is created, and it will have the same number of values as there are checkboxes selected. And the values in the array will be the values of the selected checkboxes.

If I, for instance, select checkbox number 1, 4 and 7, and I click "Create array", the content of the array will be: 1,4,7

What will the code be for that. I don´t have the javascript code, only the HTML:

HTML Code:

<input type="checkbox" name="alotofcheckboxes" value="1" />
<input type="checkbox" name="alotofcheckboxes" value="2" />
<input type="checkbox" name="alotofcheckboxes" value="3" />
<input type="checkbox" name="alotofcheckboxes" value="4" />
<input type="checkbox" name="alotofcheckboxes" value="5" />
<input type="checkbox" name="alotofcheckboxes" value="6" />
<input type="checkbox" name="alotofcheckboxes" value="7" />
<input type="checkbox" name="alotofcheckboxes" value="8" />
<input type="checkbox" name="alotofcheckboxes" value="9" />
<input type="checkbox" name="alotofcheckboxes" value="10" />

<br><br>

<input type="button" value="Create array" onclick=???????????
pradeep's Avatar, Join Date: Apr 2005
Team Leader
HTML Code:
<input type="checkbox" name="alotofcheckboxes[]" value="1" />
<input type="checkbox" name="alotofcheckboxes[]" value="2" />
<input type="checkbox" name="alotofcheckboxes[]" value="3" />
<input type="checkbox" name="alotofcheckboxes[]" value="4" />
<input type="checkbox" name="alotofcheckboxes[]" value="5" />
<input type="checkbox" name="alotofcheckboxes[]" value="6" />
<input type="checkbox" name="alotofcheckboxes[]" value="7" />
<input type="checkbox" name="alotofcheckboxes[]" value="8" />
<input type="checkbox" name="alotofcheckboxes[]" value="9" />
<input type="checkbox" name="alotofcheckboxes[]" value="10" />

<br><br>
You'll get the checked ones as an array.