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
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>
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(", "));
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.
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"));
});
<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.
Check or uncheck all · Limit checkbox selections · Checkbox validation
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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 | |