How to Check All Checkboxes with One Click in JavaScript

How to Check All Checkboxes with One Click in JavaScript

Introduction

In this tutorial we will create a How to Check All Checkboxes with One Click in JavaScript. This tutorial purpose is to teach you how to check all the checkboxes. This will cover all the basic function that will check all the checkboxes. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use checkbox in a form. I will give my best to provide you the easiest way of creating this program Check all checkboxes. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the form with checkboxes and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2.         <head>
  3.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5.         </head>
  6.  
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Check All Checkboxes with One Click</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-12">
  17.                                                        
  18.                         <h2>Select your favorite move genre</h2>
  19.                         <label><input type="checkbox" onclick="check(this);" />Select All</label>      
  20.                         <ul style="list-style-type:none;">
  21.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Action">Action</label></li>
  22.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Drama">Drama</label></li>
  23.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Comedy">Comedy</label></li>
  24.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Horror">Horror</label></li>
  25.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Sci-fi">Sci-fi</label></li>
  26.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Romance">Romance</label></li>
  27.                                 <li><label><input type="checkbox" name="movie_genre" class="select_all" value="Thriller">Thriller</label></li>
  28.                         </ul>
  29.                        
  30.                         <button type="button" class="btn btn-primary" onclick="selectedCheck();">Submit</button>
  31.                 </div>
  32.         </div>
  33.  
  34. <script src="script.js"></script>
  35. </body>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically check all the checkboxes in one click. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function check(source){
  2.         var checkboxes = document.getElementsByClassName('select_all');
  3.         for(var i in checkboxes){
  4.                 checkboxes[i].checked = source.checked;
  5.         }
  6. }
  7.  
  8. function selectedCheck(){
  9.         var selected = document.getElementsByName('movie_genre');
  10.        
  11.         var lang= [];
  12.        
  13.         for(var i in selected){
  14.                 if(selected[i].checked){
  15.                         lang.push(selected[i].value);
  16.                 }
  17.         }
  18.        
  19.         var list=lang.join(', ');
  20.        
  21.        
  22.         alert("You selected "+list+"");
  23. }

In the code above we will just create a method called check(). This function will select all the checkboxes within the form when the main checkbox is checked. The code is consist of loops that will loop through the checkboxes attribute.

Output:

The How to Check All Checkboxes with One Click in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Check All Checkboxes with One Click in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment