Step 1:Create index.html file and implement below code.
<select class="form-control limiter" data-prev-limit="1" >
<option value="1" selected>Single</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<br><br>
<!-- checkboxes -->
<div class="boxes">
<input type="checkbox" value="first" id="cbox1" />
<label for="cbox1">First option</label><br>
<input type="checkbox" value="second" id="cbox2" />
<label for="cbox2">Second option</label><br>
<input type="checkbox" value="third" id="cbox3" />
<label for="cbox3">Third option</label><br>
<input type="checkbox" value="fourth" id="cbox4" />
<label for="cbox4">Fourth option</label><br>
<input type="checkbox" value="fifth" id="cbox4" />
<label for="cbox5">Fifth option</label><br>
</div>
Step 2:Implement jQuery to enable or disable on select of dropdown.
<script>
$(document).ready( () => {
// short script to dynamically limit the amount of selectable checkboxes
let checkLimit = $(".limiter").val();
let totalChecked = 0;
$(".limiter").on("change", function(e) {
// refactor to keep selections when updating limit;
checkLimit = $(this).val();
$("input[type=checkbox]").each(function() {
$(this).prop("checked", false);
$(this).prop("disabled", false);
});
});
$(".boxes").on("change", "input[type=checkbox]", function(e) {
totalChecked = $("input[type=checkbox]:checked").length;
if (totalChecked < checkLimit) {
// keep options open and restore if unchecking option
$(this).siblings("input[type=checkbox]").each(function() {
if ($(this).not("checked")) $(this).prop("disabled", false);
});
}
if (totalChecked == checkLimit) {
// disable all empty checkboxes
$("input[type=checkbox]").each(function() {
$(this).prop("disabled", true);
});
// prevent earlier checks from being disabled..
$("input[type=checkbox]:checked").each(function (){
$(this).prop("disabled", false);
});
}
});
});
</script>
Complete Code For Enable The Checkbox Based On Selected Item In JQuery
<!DOCTYPE html>
<html>
<head>
<title>How To Enable The Checkbox Based On Selected Item In JQuery</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
body {
background: black;
}
</style>
<body>
<div class="container">
<br><br><br>
<div class="text-center">
<h1 id="color" style="color: White;">Enable The Checkbox Based On Selected Item In JQuery</h1>
</div>
<br>
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="well">
<select class="form-control limiter" data-prev-limit="1">
<option value="1" selected>Single</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<br><br>
<!-- checkboxes -->
<div class="boxes">
<input type="checkbox" value="first" id="cbox1"/>
<label for="cbox1">First option</label><br>
<input type="checkbox" value="second" id="cbox2"/>
<label for="cbox2">Second option</label><br>
<input type="checkbox" value="third" id="cbox3"/>
<label for="cbox3">Third option</label><br>
<input type="checkbox" value="fourth" id="cbox4"/>
<label for="cbox4">Fourth option</label><br>
<input type="checkbox" value="fifth" id="cbox4"/>
<label for="cbox5">Fifth option</label><br>
</div>
<script>
$(document).ready(() => {
// short script to dynamically limit the amount of selectable checkboxes
let checkLimit = $(".limiter").val();
let totalChecked = 0;
$(".limiter").on("change", function (e) {
// refactor to keep selections when updating limit;
checkLimit = $(this).val();
$("input[type=checkbox]").each(function () {
$(this).prop("checked", false);
$(this).prop("disabled", false);
});
});
$(".boxes").on("change", "input[type=checkbox]", function (e) {
totalChecked = $("input[type=checkbox]:checked").length;
if (totalChecked < checkLimit) {
// keep options open and restore if unchecking option
$(this).siblings("input[type=checkbox]").each(function () {
if ($(this).not("checked")) $(this).prop("disabled", false);
});
}
if (totalChecked == checkLimit) {
// disable all empty checkboxes
$("input[type=checkbox]").each(function () {
$(this).prop("disabled", true);
});
// prevent earlier checks from being disabled..
$("input[type=checkbox]:checked").each(function () {
$(this).prop("disabled", false);
});
}
});
});
</script>
</div>
</div>
<div class="col-md-2"></div>
</div>
</body>
</html>