Jump to content

Checkboxes And Arrays


gilbertsavier
 Share

Recommended Posts

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,7What 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=???????????

Link to comment
Share on other sites

You just need to loop through all <input> elements (using getElementsByTagName()), check the 'type' and 'name' attributes to see if it's one of the ones we're looking for, finally check if it's selected and add it to an array using the Array.push() method.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...