Mutidimensional Array Of Checkboxes?

forms, html, javascript

Solution

Well it is possible to select a specific checkbox.

You can select on the name attribute `question[x][]` then loop through those to get each of their checked values.

An example using jQuery:

var checkedBoxes = {0: [], 1: [], 2: []};
$("input[name='question[0][]']").each(function(){
    checkedBoxes[0].push(this.checked);
});
//then do the same for 1 and 2

//after everything:
console.log(checkedBoxes); //a multidimesional array of checked boxes

Or to make it even fancier:

var checkedBoxes = {0: [], 1: [], 2: []};
for(index in checkedBoxes) {
    $("input[name='question[" + index + "][]']").each(function(){
        checkedBoxes[index].push(this.checked);
    });
}
//after everything:
console.log(checkedBoxes); //a multidimesional array of checked boxes

Fiddle: http://jsfiddle.net/maniator/XA8XV/

Problem

Possible Duplicate: fetching checkbox multidimensional array in javascript Is it possible to implement a multidimensional array of checkboxes? For example ``` <input type='checkbox' name='question[0][]' value='0'> <input type='checkbox' name='question[0][]' value='1'> <input type='checkbox' name='question[0][]' value='2'> <input type='checkbox' name='question[1][]' value='0'> <input type='checkbox' name='question[1][]' value='1'> <input type='checkbox' name='question[1][]' value='2'> <input type='checkbox' name='question[2][]' value='0'> <input type='checkbox' name='question[2][]' value='1'> <input type='checkbox' name='question[2][]' value='2'> ``` If this is possible how would you pick up whether the checkboxes are checked or not in javascript?

Original source

Related problems