JavaScript Checkbox Array: Get Selected Values

Reading a group of selected checkboxes

Checkboxes with the same name form a logical group. JavaScript can collect the group, filter the boxes whose checked property is true, and read their value properties.

const boxes = document.querySelectorAll('input[name="scripts"]');
const selected = [...boxes]
  .filter((box) => box.checked)
  .map((box) => box.value);
console.log(selected);
Open checkbox group demo
Table of Contents

Create a Checkbox Group Top ↑

The original tutorial gives several checkboxes the same name. That idea is retained. If PHP must receive multiple submitted values, a name such as scripts[] is commonly used.

<label><input type="checkbox" name="scripts" value="JavaScript"> JavaScript</label>
<label><input type="checkbox" name="scripts" value="PHP"> PHP</label>
<label><input type="checkbox" name="scripts" value="HTML"> HTML</label>

Read Only the Selected Values Top ↑

The older page used document.form1.scripts.length and indexed access. That remains possible, but a selector avoids coupling the code to a form name.

const boxes = document.querySelectorAll('input[name="scripts"]');
let selected = [];
for (const box of boxes) {
  if (box.checked) {
    selected.push(box.value);
  }
}
console.log(selected.join(", "));

Use getElementsByName() Top ↑

const boxes = document.getElementsByName("scripts");
const selected = [...boxes]
  .filter((box) => box.checked)
  .map((box) => box.value);

See the getElementsByName() tutorial for the DOM method itself.

Validate the Group Before Processing Top ↑

The original demo checks a radio group and then requires at least one script checkbox. A modern submit listener keeps that behavior without inline event code.

form.addEventListener("submit", (event) => {
  event.preventDefault();
  const gender = document.querySelector('input[name="gender"]:checked');
  const scripts = [...document.querySelectorAll('input[name="scripts"]:checked')];
  if (!gender) {
    alert("Please select a gender option.");
    return;
  }
  if (scripts.length === 0) {
    alert("Select at least one script.");
    return;
  }
  alert(scripts.map((box) => box.value).join("\n"));
});

Submit Multiple Checkbox Values to PHP Top ↑

<label><input type="checkbox" name="scripts[]" value="JavaScript"> JavaScript</label>
<label><input type="checkbox" name="scripts[]" value="PHP"> PHP</label>

JavaScript validation does not replace server-side validation. The existing PHP checkbox-array tutorial covers server-side handling.

Reading a group of checkbox values in JavaScript

Check or uncheck all · Limit checkbox selections · Checkbox validation




Subscribe to our YouTube Channel here



plus2net.com







Nagarjuna

03-12-2012

(BY USING JAVA SCRIPT)In my application i have one required i,e I have multiple number of check boxes(suppose 10 boxes). when i am going to click a button like lucky dip ,it has to select 6 random check boxes automatically (when i click same button for another time i want to get different 6 values from 10 initial values apart from first values). (I am able to checked 6 boxes(not different values) from 10 check boxes ) could you please give me some suggestions .
yash

03-05-2013

hii.. i m yash... sorry for first comment...

hii i have some problem. plz suggest me the write way to solve my querru.... i have a form .. with multiple checkbox and a search button.... i want when no checkbox is selected then the search button is disappear ... i any one is selected then this search button show... plz send me a code for this...
thanx



✖
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