JavaScript How to disable unselected checkboxes when a limit is reached disable unchecked checkbox jquery,disable unchecked checkbox javascript,unchecked disabled

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
   }
 });
Answer:1

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[]" />
Answer:2

I have developed this neat little piece of code which given an element and a properly formatted table, creates a dropdown menu on the element which allows the user to toggle view of columns. I cannot ...

I have developed this neat little piece of code which given an element and a properly formatted table, creates a dropdown menu on the element which allows the user to toggle view of columns. I cannot ...

  1. javascript html table to csv
  2. javascript html table library
  3. javascript html table to json
  4. javascript html table to excel
  5. javascript html table to array
  6. javascript html table sort
  7. javascript html table filter
  8. javascript html table pagination
  9. javascript html table add row
  10. javascript html table search
  11. javascript html table example
  12. javascript html table generator
  13. javascript html table insert row
  14. javascript html table column width
  15. javascript html table to xlsx
  16. javascript html table to pdf
  17. javascript html table to excel xlsx
  18. javascript html table delete all rows
  19. javascript html table
  20. javascript html table export to excel

To understand async/await, I am trying to display a console message once settimeout runs and expires. How do I fix my code below? I have 5 settimeout function and each should display respective ...

To understand async/await, I am trying to display a console message once settimeout runs and expires. How do I fix my code below? I have 5 settimeout function and each should display respective ...

  1. using async await
  2. using async await c#
  3. using async await javascript
  4. using async await in node js
  5. using async await in react
  6. using async await mongoose
  7. using async await in node
  8. using async await axios
  9. using async await and promises
  10. using async await fetch
  11. use async await in angular 6
  12. using async without await
  13. using async await with callback functions
  14. async with await
  15. async with await python
  16. async with await c#
  17. use effect async await
  18. why use async await in web api

Hi i'm using Jquery Upload plugin. I've got this problem: when i upload the first file this work fine,a single request is sent. But if i select a second file and i upload it,two request are sent and ...

Hi i'm using Jquery Upload plugin. I've got this problem: when i upload the first file this work fine,a single request is sent. But if i select a second file and i upload it,two request are sent and ...

  1. when do we choose another president

I've tried to render a simple component but cant seem to make it work. <Col md={{ span: 16, offset: 4 }} xs={{ span: 20, offset: 2 }}> {props.language === "en" ? <englishReport /> : &...

I've tried to render a simple component but cant seem to make it work. <Col md={{ span: 16, offset: 4 }} xs={{ span: 20, offset: 2 }}> {props.language === "en" ? <englishReport /> : &...