4

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?

Community
  • 1
  • 1
cjds
  • 8,268
  • 10
  • 49
  • 84
  • 3
    Yes, the values of those checkboxes would appear as a multi-dimensional array at the receiving end (serverside). No, those checkboxes won't turn into a multi-dimensional in Javascript. You have to access each checkbox individually and determine if it's checked or not. – Nadh Nov 24 '12 at 12:39
  • document.getElementById("questionForm").elements['question[][]'][i][j].checked Will that work? – cjds Nov 24 '12 at 12:56
  • 3
    No, that won't work. The `[]` in the checkbox's name attribute is simply its name. It doesn't translate to an array in the DOM. – Nadh Nov 24 '12 at 13:14
  • 2
    Note however that PHP has special feature/support for parameter names ending with braces. PHP will "automagically" convert them to a single array when `$_POST['question']` is accessed. JavaScript and other server side languages like JSP don't do that. How to access it in JSP/Servlet is demonstrated in among others http://stackoverflow.com/a/13087174 and http://stackoverflow.com/a/4902050 It's not unsurprising that this PHP-specific feature confuses starters when attempting to do the same in other languages. – BalusC Dec 14 '12 at 14:52
  • @CarlSaldanha see my update. I added an alternative "fancier" option ^_^ – Naftali Dec 14 '12 at 16:10

1 Answers1

4

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/

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 1
    jQuery, lame. Why not via `document.querySelectorAll`? – hakre Dec 14 '12 at 16:12
  • @hakre I am not very adept at [vanilla js](http://vanilla-js.com/), but that would most probably be the way to do it ^_^ (but it might not work in all browsers...) – Naftali Dec 14 '12 at 16:13