javascript - How to display an alert when only three of nine checkboxes are checked? -
i have implemented following code website produce alert when 3 checkboxes named "correct" checked.
<input type="checkbox" name="correct"/> <input type="checkbox" name="correct"/> <input type="checkbox" name="correct"/> const correctinputs = [...document.queryselectorall('input[name="correct"]')]; const alertifthree = () => { const checkedcorrectinputs = correctinputs.filter(input => input.checked); if (checkedcorrectinputs.length > 2) { alert('alert'); } }; correctinputs.foreach(input => input.addeventlistener('click', alertifthree)); however, have 6 checkboxes named "incorrect" , when of these checkboxes checked don't want alert produced, if 3 "correct" checkboxes checked.
<input type="checkbox" name="incorrect"/> <input type="checkbox" name="incorrect"/> <input type="checkbox" name="incorrect"/> <input type="checkbox" name="incorrect"/> <input type="checkbox" name="incorrect"/> <input type="checkbox" name="incorrect"/> how can change code implement this? in advance!
you can keep same logic
const correctinputs = [...document.queryselectorall('input[name="correct"]')]; const incorrectinputs = [...document.queryselectorall('input[name="incorrect"]')]; const alertifthree = () => { const checkedcorrectinputs = correctinputs.filter(input => input.checked); const checkedincorrectinputs = incorrectinputs.filter(input => input.checked); if (incorrectinputs.length === 0 && checkedcorrectinputs.length > 2) { alert('alert'); } };
Comments
Post a Comment