I have an HTML form that contains a set of checkboxes:
<p><input type="checkbox" name="fruits" value="apple" /> Apple</p>
<p><input type="checkbox" name="fruits" value="banana" /> Banana</p>
<p><input type="checkbox" name="fruits" value="strawberry" /> Strawberry</p>
When I click a button I want to have all the selected checkboxes with the same name in one array.
So, if the user selects Apple and Banana I expect something like this:
{ 'fruits' : [ 'Apple', 'Banana' ] }
I tried to use jQuery's serializeArray() method but the result is not as I expected...
[ { name="fruits", value="apple"}, { name="fruits", value="banana"} ]
I've created this jsfiddle with my example code.
I know I could iterate over this array and create the object I want. But my question is:
Is there a clean & simple way (using jQuery or some plugin) to do this?
*Edit: * I'm looking for some kind of generic solution to this problem. I'd like to tell a method the form element and have that method automatically find all data. Just like the jQuery.serializeArray() does.