Check or Uncheck All Checkboxes with JavaScript

To select or clear a whole checkbox group, collect the group and assign the same Boolean value to each box’s checked property.

const boxes = document.querySelectorAll('input[name="check_list"]');
function setAll(checked) {
  boxes.forEach((box) => {
    box.checked = checked;
  });
}

If JavaScript is unavailable, the group controls will not run; the existing JavaScript support guide explains browser-side script support.

Open check/uncheck-all demo
Table of Contents

Use Separate Check All and Uncheck All Buttons Top ↑

<button type="button" id="checkAll">Check All</button>
<button type="button" id="uncheckAll">Uncheck All</button>
const boxes = document.querySelectorAll('input[name="check_list"]');
function setAll(checked) {
  boxes.forEach((box) => {
    box.checked = checked;
  });
}
document.getElementById("checkAll").addEventListener("click", () => setAll(true));
document.getElementById("uncheckAll").addEventListener("click", () => setAll(false));

Use One Master Checkbox Top ↑

The original page also uses one checkbox to manage a group. Copy the master checkbox’s state to every child checkbox.

const master = document.getElementById("checkAllControl");
const boxes = document.querySelectorAll('input[name="check_list"]');
master.addEventListener("change", () => {
  boxes.forEach((box) => {
    box.checked = master.checked;
  });
});

When individual boxes can change, the master control can also use indeterminate to show partial selection.

Submitting the Selected Checkbox Group Top ↑

For a PHP form that receives multiple values, use a name such as check_list[]. The browser submits only checked, enabled checkboxes.

<label><input type="checkbox" name="check_list[]" value="PHP"> PHP</label>
<label><input type="checkbox" name="check_list[]" value="JavaScript"> JavaScript</label>
Check or uncheck a group of checkboxes with JavaScript

jQuery Alternative Top ↑

Native DOM APIs are sufficient for this task, but the original link to the jQuery checkbox tutorial remains useful for projects already using jQuery.

Single-control toggle patterns · Limit selections · Read selected values




Subscribe to our YouTube Channel here



plus2net.com







Maria

18-11-2010

Thank you! This is exactly what I was looking for!
Mawimus

18-05-2011

Thank you ! But, how you get values? you have only the last value by the last checkbox checked?? !!
Michael

07-08-2011

Get values by chk[i].value and set values by chk[i].value = "value";
Firdaous Nizami

26-12-2011

thanks for information, and can u understand/help me about check box while i am using it in struts framework. i.e, how can i fetch only selected items after submit of the page. please guide me if you can...
Alexander

05-01-2012

Can I use something different than "name" attribute to identify the checkbox? For example id or class or stuff? As I need to have name attribute as php array. Email me please, would be really grateful.
cadetcaptain

25-01-2012

Thank you! Nice 1!
Jack

24-02-2012

Nice Tutorial!!!!
Phiri

02-06-2012

Does this work without forms? What if I want to check checkboxs with the same name in a specific div tag?
Dhanapal

10-08-2012

ya..........it`s works........... thank ypu
tonbad

11-08-2012

many many thanks...
ssol

22-11-2012

If you want to get all Checked values in form name should be name="check-list[]".
minm

22-08-2014

Thank you it worked
Adam

06-10-2014

I am also using the name field for a php array. It seems to work fine if you just set the id field on the checkbox to id="check_list" and leave your name field as-is.
jjj

03-02-2015

Thank you so much...i was struggling alot to check all boxes whixh were in while loop ans displaying resultset but this solution is working for me.....
happy coding:)
Dileep Soni

30-05-2015

Thank you so much for nice Tutorial!!!!
Dharmendra Kumar

05-06-2015

Hey, I want to check and uncheck all check boxes in gridview (asp.net) by button click.
Pls someone help me....
Thanks
belajarhebat

04-08-2015

thanks for information. good job :) !!
AAAA

07-08-2015

Thank you Very Nice Explanation...........
sohel

28-09-2015

function UnCheckAll does not have ' { ' other than that nice explanation! Cheers!!!
smo1234

09-10-2015

thanks , added now.



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