I am working on a form that contains checkbox groups, structured like this:
<input type="checkbox" name="group_one" value="1">
<input type="checkbox" name="group_one" value="2">
<input type="checkbox" name="group_two" value="1">
<input type="checkbox" name="group_two" value="2">
My goal is to create an array with keys representing the checkboxes' names and values as arrays of their selected values when the form changes. Here is the JavaScript code I have so far:
var checkboxes = $('input').filter(':checked');
var myArray = [];
checkboxes.each(function () {
var name = $(this).attr('name');
var val = $(this).val();
myArray[name] = val;
});
The desired outcome is to have an array where 'name' serves as keys and 'val' as arrays, similar to this format:
[group_one: {1,2}, group_two: {1,2}]
I hope this explanation makes sense. Thank you in advance for any assistance!