2

I would like to know how to disable unselected checkboxes using jQuery. The goal is to disable the unselected checkboxes if the number of selected checkboxes is greater than or equal to 3.

 $('.myCheckBox').change(function() {
   var checkBoxLenghtStandard = $('[name="addon-2811-workshop-normal-    1[]"]:checked').filter(':checked').length;

   if (checkBoxLenghtStandard >= 3) {
     //Here I would like to disable unselected checkboxes
   }
 });
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user10087184
  • 57
  • 2
  • 9

3 Answers3

3

To make this work you can use the :not(:checked) selector to filter the available unchecked boxes, the prop() to disabled them.

Note that you will also need an else condition to enable the checkboxes again when one is deselected.

var $checkboxes = $('.myCheckBox').change(function() {
  var $checked = $checkboxes.filter(':checked'); 
  if ($checked.length >= 3) {
    $checkboxes.filter(':not(:checked)').prop('disabled', true);
  } else {
    $checkboxes.prop('disabled', false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
<input type="checkbox" class="myCheckBox" name="addon-2811-workshop-normal-1[]" />
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
3

Try - we set disable flag to unchecked inputs

$('input').click(function() {
  checkboxesValidate();
})

function checkboxesValidate() {
 $('input:not(:checked)').attr("disabled", $('input:checked').length === 3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='checkbox'><input type='checkbox'><input type='checkbox'><input type='checkbox'><input type='checkbox'><input type='checkbox'><input type='checkbox'><input type='checkbox'>
0

You could use:

$(".myCheckBox:not(:checked)").prop('disabled', true);

To disable all checkboxes which are not checked.

You can then reenable your checkboxes (in an else) once fewer than 3 checkboxes have been selected

See example below:

$('.myCheckBox').on('input', function() {
  var checkBoxLenghtStandard = $('.myCheckBox:checked').length;
  if (checkBoxLenghtStandard >= 3) {
    $(".myCheckBox:not(:checked)").prop('disabled', true);
  } else {
    $('.myCheckBox[disabled]').prop('disabled', false)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="myCheckBox" type="checkbox" name="addon-2811-workshop-normal-1[]" />

<input class="myCheckBox" type="checkbox" name="addon-2811-workshop-normal-1[]" />

<input class="myCheckBox" type="checkbox" name="addon-2811-workshop-normal-1[]" />

<input class="myCheckBox" type="checkbox" name="addon-2811-workshop-normal-1[]" />

<input class="myCheckBox" type="checkbox" name="addon-2811-workshop-normal-1[]" />
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64