1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Checkboxes and arrays

Discussion in 'JavaScript and AJAX' started by gilbertsavier, Aug 5, 2009.

  1. gilbertsavier

    gilbertsavier New Member

    Hi,
    I need some code which can give an array some values, depending on which checkboxes which is selected.
    I think I will give these checkboxes (lets say 10 checkboxes) some values, and when you have selected the relevant checkboxes, you will have to click a button: "Create array". Then a array is created, and it will have the same number of values as there are checkboxes selected. And the values in the array will be the values of the selected checkboxes.

    If I, for instance, select checkbox number 1, 4 and 7, and I click "Create array", the content of the array will be: 1,4,7

    What will the code be for that. I donĀ“t have the javascript code, only the HTML:

    HTML Code:

    <input type="checkbox" name="alotofcheckboxes" value="1" />
    <input type="checkbox" name="alotofcheckboxes" value="2" />
    <input type="checkbox" name="alotofcheckboxes" value="3" />
    <input type="checkbox" name="alotofcheckboxes" value="4" />
    <input type="checkbox" name="alotofcheckboxes" value="5" />
    <input type="checkbox" name="alotofcheckboxes" value="6" />
    <input type="checkbox" name="alotofcheckboxes" value="7" />
    <input type="checkbox" name="alotofcheckboxes" value="8" />
    <input type="checkbox" name="alotofcheckboxes" value="9" />
    <input type="checkbox" name="alotofcheckboxes" value="10" />

    <br><br>

    <input type="button" value="Create array" onclick=???????????
     
  2. pradeep

    pradeep Team Leader

    HTML:
    <input type="checkbox" name="alotofcheckboxes[]" value="1" />
    <input type="checkbox" name="alotofcheckboxes[]" value="2" />
    <input type="checkbox" name="alotofcheckboxes[]" value="3" />
    <input type="checkbox" name="alotofcheckboxes[]" value="4" />
    <input type="checkbox" name="alotofcheckboxes[]" value="5" />
    <input type="checkbox" name="alotofcheckboxes[]" value="6" />
    <input type="checkbox" name="alotofcheckboxes[]" value="7" />
    <input type="checkbox" name="alotofcheckboxes[]" value="8" />
    <input type="checkbox" name="alotofcheckboxes[]" value="9" />
    <input type="checkbox" name="alotofcheckboxes[]" value="10" />
    
    <br><br>
    You'll get the checked ones as an array.
     
    shabbir likes this.

Share This Page