Check and Uncheck all Checkbox using JavaScript

Check and Uncheck all Checkbox using JavaScript
Code Snippet:check/uncheck checkbox with pure javascript
Author: Elvis
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 6,371
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to check and uncheck all checkbox using three different methods. You can integrate any method to allow users to select and deselect all checkboxes at once.

The first method uses the toggle function to check and uncheck a list of checkboxes. The second function uses separate buttons for check all and uncheck all. Similarly, the third JS function uses a reset button to uncheck all checkboxes in a from.

How to Check and Uncheck all Checkbox in JavaScript

1. Create the HTML structure for checkboxes as follows:

<div class="container">
   <div class="row">
      <div class="col-sm-4">
         <form>
            <legend>Version 1<small>Single buttons</small></legend>
            <div class="checkbox">
               <input type="button" id="checkall1" class="btn btn-link" value="Un/check All">
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check1"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check1"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check1"> Check me out
               </label>
            </div>
         </form>
      </div>
      <div class="col-sm-4">
         <form>
            <legend>Version 2<small>Separate buttons</small></legend>
            <div>
               <input type="button" onclick="checkAll2()" class="btn btn-link" value="Check All">
               <input type="button" onclick="uncheckAll2()" class="btn btn-link" value="Uncheck All">
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check2"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check2"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check2"> Check me out
               </label>
            </div>
         </form>
      </div>
      <div class="col-sm-4">
         <form>
            <legend>Version 3<small>Reset button</small></legend>
            <div>
               <input type="button" onclick="checkAll3()" class="btn btn-link" value="Check All">
               <input type="reset" class="btn btn-link" value="Uncheck All">
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check3"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check3"> Check me out
               </label>
            </div>
            <div class="checkbox">
               <label>
               <input type="checkbox" class="check3"> Check me out
               </label>
            </div>
         </form>
      </div>
   </div>
</div>

2. Basically, there is no style for this project. You can define your own CSS styles.

legend small {
  display: block;
  font-size: 12px;
}

3. Finally, use the following JavaScript function to check and uncheck all checkboxes.

// v1
function checkAll1() {

  var inputs = document.querySelectorAll('.check1');
  for (var i = 0; i < inputs.length; i++) {
    inputs[i].checked = true;
  }

  this.onclick = uncheckAll1;
}

function uncheckAll1() {
  var inputs = document.querySelectorAll('.check1');
  for (var i = 0; i < inputs.length; i++) {
    inputs[i].checked = false;
  }

  this.onclick = checkAll1; //function reference to original function
}

var el = document.getElementById("checkall1"); //let for ES6 aficionados 
el.onclick = checkAll1; //again, function reference, no ()

//v2
function checkAll2() {
  var inputs = document.querySelectorAll('.check2');
  for(var i = 0; i < inputs.length; i++) {
    inputs[i].checked = true;
  }
}

function uncheckAll2() {
  var inputs = document.querySelectorAll('.check2');
  for(var i = 0; i < inputs.length; i++) {
    inputs[i].checked = false;
  }
}

window.onload = function() {
  window.addEventListener('load', checkAll2, false);
}


//v3
function checkAll3() {
  var inputs = document.querySelectorAll('.check3');
  for(var i = 0; i < inputs.length; i++) {
    inputs[i].checked = true;
  }
}

window.onload = function() {
  window.addEventListener('load', checkAll3, false);
}

That’s all! hopefully, you have successfully integrated this check and uncheck all checkbox functionality into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X