I have an HTML form with a number of checkboxes that all have the same class="shared". I use the following to limit the number of checkable checkboxes to 3 at a time which works as intended:
$(document).on('change', '.shared', function() {
var countShared = $('.shared:checked').length;
if(countShared > 3 && $(this).is(':checked')) {
alert("You can only select up to 3 departments to share with.");
$(this).prop('checked',false);
}
});
Now my problem is the following:
I have 3 variables, shared1, shared2 and shared3 and want to assign the values from the checked checkboxes above to these variables. I am looking for something that assigns the value of the 1st checked checkbox to variable shared1, the value of the 2nd checked checkbox to shared2 and the value of the 3rd checked checkbox to shared3.
Usually I would try an .each loop for something like this and go by the class .shared but I don't know how to apply this here as there could also be less than 3 or no checkboxes checked and I don't know which ones are.
How can I realise this ?