0

So I have a few lists of checkboxes, all with the a slightly different class of single-check with a number on the end indicating which list they're in, since I'm not using radio buttons. How would I go about making it so that any checkbox that is in the same class can't be checked if another checkbox in that class is already checked?

Thanks

Johnson Gale
  • 155
  • 1
  • 11

2 Answers2

2

Using jQuery.not when a checkbox is clicked, you could set the checked property on all the other checkboxes in that group, to false (meaning they won't be checked).

$(function() {
    $(".myCheckboxInGroup1").on("click", function() {
        $(".myCheckboxInGroup1").not(this).prop("checked", false);
    });
});

Example: http://codepen.io/Gigabyte-Giant/pen/wWavZj

Brynden
  • 35,463
  • 2
  • 15
  • 21
0

function howtoreadresults(){
   var r = [];
   $('input[type=checkbox]:checked').each(function(i,o){
      r.push($(o).attr('group')+'='+$(o).val());
   });
   return r;
}

$('input[type=checkbox]').change(function(e){
   $('[group='+$(this).attr('group')+']:checked').not($(this)).prop('checked',false);
   console.log(howtoreadresults());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<input type='checkbox' group='cars' value='red' />
<input type='checkbox' group='cars' value='blue' />
<input type='checkbox' group='cars' value='green' />
<input type='checkbox' group='bike' value='red' />
<input type='checkbox' group='bike' value='blue' />
<input type='checkbox' group='bike' value='green' />
christian
  • 538
  • 5
  • 8