Javascipt - Put checked checkboxes into an array

icscrew

New Member
TO CLARIFY I'VE GOT THE VALUES OF THE CHECKED CHECKBOXES. I NEED TO ORGANIZE THEM IN AN ARRAY SIMILAR TO THE PHP EXAMPLE GIVEN. THANKSI'm trying to build an array of checked checkboxes to be sent over ajax for processing by PHP. Originally I got the selected checkboxes by PHP but I'm having trouble converting this process to Javascript.There are three different groups of checkboxes ('first', 'second', 'third') and for each of these groups there are 4 different checkboxes that can be checked.The html of the checkboxes followings the same pattern up to value 4.\[code\]<input type="checkbox" name="first" class="selection first" value="http://stackoverflow.com/questions/11099592/1" /><input type="checkbox" name="second" class="selection second" value="http://stackoverflow.com/questions/11099592/1" /><input type="checkbox" name="third" class="selection third" value="http://stackoverflow.com/questions/11099592/1" /><input type="checkbox" name="first" class="selection first" value="http://stackoverflow.com/questions/11099592/2" /><input type="checkbox" name="second" class="selection second" value="http://stackoverflow.com/questions/11099592/2" /><input type="checkbox" name="third" class="selection third" value="http://stackoverflow.com/questions/11099592/2" />\[/code\]and so on....The PHP would return an array of the checked checkboxes like such.\[code\]$selections => array(['first'] => array([0] => 1,[1] => 3,[2] => 4),['second'] => array([0] => 2),['third'] => array([0] => 1,[1] => 4));\[/code\]I've been trying and trying to replicate this in JavaScript but I keep getting errors such as cannot convert undefined to object and such so I loop the available positions to create arrays and then loop all the checkboxes and if checked I get the value.The console.log returns the position ('first', 'second' or 'third') and the number of the selection (1 - 4). That's all I need, but how do I put this in an array similar to the php one?\[code\]function getSelections() {var selections = new Array();$('.position').each(function() { selections[$(this).attr('value')] = new Array;});$('.selection').each(function() { if( $(this).prop('checked') == true ) { position = $(this).attr('name'); selection = $(this).attr('value'); console.log(position + ' - ' + selection); }});return selections;\[/code\]}Its to be sent to a php script as json and I need it to decode as an array like the php one.Thanks in advance for any help! I'm well and truly stuck with this!
 
Top