send checkbox array in javascript function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realin
    Contributor
    • Feb 2007
    • 254

    send checkbox array in javascript function

    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="modalDat a" id="multipleCom mSelect" style="padding: 50px;">

    <center>Pleas e 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" >Decentralizati on</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="referC omm('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
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

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

    [CODE=javascript]/**
    * returns an array with the values of the checked checkboxes with name
    * @param name name of the checkboxes
    * @return new_arr array with checked-values
    */
    function get_array_for_c heckboxes_with_ name(name) {
    var new_arr = [];
    var nodes = document.getEle mentsByName(nam e);

    for (var i = 0, n; n = nodes[i]; i++) {
    if (n.checked == true) {
    new_arr.push(n. value);
    }
    }

    return new_arr;
    }[/CODE]
    kind regards

    Comment

    • realin
      Contributor
      • Feb 2007
      • 254

      #3
      thanks gits
      i was able to do it :)

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

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

        kind regards

        Comment

        Working...