I have a checkbox that, when clicked, will cause an entire section of checkboxes to become disabled and any checkbox in that section that is checked will become unchecked. This is my code for the checkbox:
<input class="form-check-input" id="notInterest" type="checkbox" onclick="NoInterest(this)"> Not Interested
If the above checkbox becomes checked, then I want an entire section of checkboxes to be disabled and unchecked.
<input class="form-check-input" type="checkbox" onclick="NoInterest(this)"> Not Intereted
<div class="form-group row" id="reassignChecks">
@for (var i = 1; i <= 10; i++)
{
<input name="AreChecked" type="checkbox" value="@i" /> @i
}
</div>
This is the code that disables the section of checkboxes on the click of the "Not Interested" checkbox. Disabling of the checkboxes is working fine, but unchecking of the checkboxes is not working. Below is the jQuery code:
<script>
function NoInterest(cb) {
if (cb.checked == true) {
$('#reassignChecks').addClass('disableSection');
$('#reassignChecks').attr('checked', false);
} else {
$('#reassignChecks').removeClass('disableSection');
$('#reassignChecks').attr('checked', true);
}
}
</script>
Below is the screen shot of the web page:
In the above screen shot, on the click of the "Not Interested" checkbox, I want all the checkboxes in the div tag with id "reassignChecks" to become disabled and unchecked.
2
Answers
there are 2 things to tell =>
1- you can check if there are checked inputs by class-name =>
2- detect if an input is checked or not =>
3- finally see the below snippet =>
$('#reassignChecks').attr('checked', false);
will not affect the checkboxes because#reassignChecks
is a<div>
.Instead you want to select all of the
input[type="checkbox"]
elements that are children of#reassignChecks
and set theirdisabled
attribute according to the checked/unchecked state of.form-check-input
, as well as, presumably, set each of their:checked
properties tofalse
.