Check & Uncheck All Checkboxes With A Single Click – SelectAllCheckbox

Category: Image | September 5, 2020
Views Total:4,486 views
Official Page:Go to website
Last Update:September 5, 2020


Check & Uncheck All Checkboxes With A Single Click – SelectAllCheckbox


This is the Vanilla JavaScript version of the jQuery SelectAllCheckbox plugin that adds check all and uncheck all functionalities to checkbox groups.

Click on the ‘master’ checkbox to select or deselect all slave checkboxes.

How to use it:

1. Insert the selectallcheckbox.js script into the document.

<script src="./src/selectallcheckbox.js"></script>

2. Create a Select All checkbox for your checkbox group.

<label class="selectAllLabel">
  <input type="checkbox" id="selectAllGroup1"/>
  Select All
  <input type="checkbox" id="group1_a" name="group1" value="1a" />
  Choice A
  <input type="checkbox" id="group1_b" name="group1" value="1b" />
  Choice B
  <input type="checkbox" id="group1_c" name="group1" value="1c" />
  Choice C

3. Initialize the library and pass the following parameters to the CheckboxGroup().

  • selectAllId: The HTML “id” attribute of the checkbox that controls the group
  • groupName: The HTML “name” attribute of the checkbox group
  • onChangeCallback: A user-defined function to execute when a checkbox in the group changes state. The function must accept two arguments: the first is an array of the changed checkboxes as jQuery objects. The second argument is a string representing the checked state of the checkbox group: possible values are “none”, “some” and “all”.
const example = new CheckboxGroup(
function onChange( checkboxes, checkedState ) {
  var html = "<p>Changed:<br/>";
  for( var i = 0; i < checkboxes.length; i++ ) {
     var box = checkboxes[i];
     var line = + " : " + ( box.checked ? "checked" : "unchecked" ) + "<br/>";
     html += line;
  html += ("</p><p>Checked state of group is: " + checkedState + "</p>");
  document.getElementById( "log" ).innerHTML = html;

You Might Be Interested In:

Leave a Reply