LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Add Check/Uncheck All Checkbox Input Button Using Pure JavaScript

Add Check/Uncheck All Checkbox Input Button Using Pure JavaScript

Add-Check-Uncheck-All-Checkbox-Input-Button-Using-Pure-JavaScript
This JavaScript code snippet helps you to check and uncheck all checkboxes 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 checking all and unchecking all. Similarly, the third JS function uses a reset button to uncheck all checkboxes in a form.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Check/Uncheck All Checkbox Input Button Using Pure JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
Make A Link That Will Open With The Double Click

CSS:

<style type="text/css">
legend small {
display: block;
font-size: 12px;
}
</style>

HTML:

<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>

JavaScript:

<script type="text/javascript">
// 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);
}
</script>

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *