views:

27

answers:

1

Hi guys, I am trying to collect tick box values and assign the ticked boxes values to a hidden field so that I can save all of the ticked boxes values into one column in a comma delimited format, instead of many.

How could I go about doing that?

Thanx in advance!

+2  A: 

You can use the map and join function like this:

var vals = $(':checkbox:checked').map(function(){
   return $(this).val();
}).get().join(',');

// save the values to a hidden field
$('#hidden_id').val(vals);
Sarfraz
Awesome! Thank you so much! Is there a way to limit the amount of boxes ticked to three? So if they check three, it will disable the rest, but if they deselect one they become clickable again. Is there a jquery function to count the amount of boxes ticked? I tried to modify the above code to accomplish that but it didn't work.
To count checked/ticked checkboxes, use: `alert($(':checkbox:checked').size());` Note the code in my answers only gets the value of checked/ticked checkboxes. Now that you know how to get the number of ticked checkboxes, you can modify the code as per your requirements if you want, have a nice time :)
Sarfraz
Awesome, will do! Thanx!
Thanx, got it working, here is my code, function whoop(){ var vals = $('#checkOnes').find(':checkbox:checked').map(function(){ return $(this).val(); }).get().join(','); alert(vals); // save the values to a hidden field $('#hidden_id').val(vals);}; Thanx Sarfraz, sorry, didn't mean to mooch off of you, still learning jQuery, so I am still a lil hesitant to go out and try things on my own before asking out about what I am trying to accomplish! Anyways, thanx again!