I'm working on a checklist app which requires a maximum of (3) check fields to be selected by the user. I want to block off any more than (3) selections. I've created a function to do this which is reading that an item has been checked, however the function doesn't block off the users ability to check more than 3 checkboxes.
const checks = document.querySelectorAll(".item-list");
let max = 3
checks.forEach(function (check) {
check.addEventListener('change', function () {
if (check.length > max) {
return false
}
})
});
<div class="form">
<p> <u> Please Select the Items Below. (A Maximum of (3) can be selected) </u> </p>
<br>
<div class="specializations-list">
<div class="list-items">
<input
class="item-list"
type="checkbox"
value ="Item-1">Item 1
<br>
<input
class="item-list"
type="checkbox"
value="Item-2">Item 2
<br>
<input
class="item-list"
type="checkbox"
value="Item-3" >Item 3
<br>
<input
class="item-list"
type="checkbox"
value="Item-4" >Item 4
<br>
<input
class="item-list"
type="checkbox"
value="Item-5" >Item 5
<br>
<input
class="item-list"
type="checkbox"
value="Item-6" >Item 6
<br>
<input
class="item-list"
type="checkbox"
value="Item-7" >Item 7
</div>