By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

send checkbox array in javascript function

realin
100+
P: 254
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 !
Apr 29 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,353
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
Apr 29 '08 #2

realin
100+
P: 254
thanks gits
i was able to do it :)
Apr 29 '08 #3

gits
Expert Mod 5K+
P: 5,353
glad to hear that ... ;) post back to the forum anytime you have more questions ...

kind regards
Apr 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.