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
}
});
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[]" />
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 ...
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 ...
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 ...
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 /> : &...