Demo: Limit Checkbox Selection by Sum of Values

Select any combination whose total value does not exceed 10. If a new selection would make the sum larger than 10, that selection is undone.

Sum: 0

JavaScript Top ↑

const boxes = [...document.querySelectorAll('input[name="ckb"]')];
const maximum = 10;
function currentSum() {
  return boxes
    .filter((box) => box.checked)
    .reduce((sum, box) => sum + Number(box.value), 0);
}
boxes.forEach((box) => {
  box.addEventListener("change", () => {
    if (currentSum() > maximum) {
      box.checked = false;
    }
  });
});

HTML Top ↑

<label><input type="checkbox" name="ckb" value="1"> Value = 1</label>
<label><input type="checkbox" name="ckb" value="2"> Value = 2</label>
<label><input type="checkbox" name="ckb" value="3"> Value = 3</label>
<div id="msg" aria-live="polite">Sum: 0</div>
Limit checkbox selections by the sum of values

Checkbox limit tutorial Count-limit demo




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer