Connecting Tech Pros Worldwide Forums | Help | Site Map

send checkbox array in javascript function

realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#1: Apr 29 '08
Hiya all,

i am in deep trouble, i thought it was an easy task, but now its getting on my nerves.
Well i have a div on a form, which contains a number of checkboxes, as

[HTML]<div class="modalData" id="multipleCommSelect" style="padding: 50px;">

<center>Please select the communities from the list below:<br></center>

<label><input name="CommID[]" value="3" type="checkbox">AIDS</label><br>
<label><input name="CommID[]" value="4" type="checkbox">Decentralization</label><br>
<label><input name="CommID[]" value="5" type="checkbox">Disaster Management</label><br>
<label><input name="CommID[]" value="6" type="checkbox">Food and Nutrition Security</label><br>
<label><input name="CommID[]" value="7" type="checkbox">Education</label><br>
<label><input name="CommID[]" value="8" type="checkbox">Gender</label><br>

<label><input name="CommID[]" value="9" type="checkbox">ICT for Development</label><br>
<label><input name="CommID[]" value="10" type="checkbox">Maternal and Child Health</label><br>
<label><input name="CommID[]" value="11" type="checkbox">Microfinance</label><br>
<label><input name="CommID[]" value="12" type="checkbox">Water</label><br>
<label><input name="CommID[]" value="13" type="checkbox">Work and Employment</label><br>
<input value="Refer to selected communities" onclick="referComm('CommID');" type="button">

</div>[/HTML]

i want to send this array of checkboxes to a javascript function. But i am unable to do it. Please can anyone help me. There on i want to run some ajax function to store this info in DB, but i am unable to pass this checkbox array ..

pleaseee help me
regards
Realin !

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Apr 29 '08

re: send checkbox array in javascript function


hi ...

you have to loop through the nodes with the name 'CommID[]' and build a javascript array from it ... like in the following example:

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * returns an array with the values of the checked checkboxes with name
  3.  * @param name name of the checkboxes
  4.  * @return new_arr array with checked-values
  5.  */
  6. function get_array_for_checkboxes_with_name(name) {
  7.     var new_arr = [];
  8.     var nodes = document.getElementsByName(name);
  9.  
  10.     for (var i = 0, n; n = nodes[i]; i++) {
  11.         if (n.checked == true) {
  12.             new_arr.push(n.value);
  13.         }
  14.     }
  15.  
  16.     return new_arr;
  17. }
kind regards
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#3: Apr 29 '08

re: send checkbox array in javascript function


thanks gits
i was able to do it :)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#4: Apr 29 '08

re: send checkbox array in javascript function


glad to hear that ... ;) post back to the forum anytime you have more questions ...

kind regards
Reply