Checkboxes and arrays

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

  1. gilbertsavier

    gilbertsavier New Member

    Joined:
    Jun 25, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice