JavaScript - Check All Checkbox With One Click

In this tutorial we will create a Check All Checkbox With One Click using JavaScript. This code will dynamically toggle all the checkboxes when user click the checkbox. The code use javascript onclick() function to call a function that will toggle all the check box form with just one click by binding each element in a class, then looping it to fetch all the checkboxes properties in order to tick the dot notation checked. This is a free program, you can modify it and use it as your own. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">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">JavaScript - Check All Checkbox With One Click</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  20. <h4>Select a Programming Language</h4>
  21. <label><input type="checkbox" onclick="check(this);" />Check All</label>
  22. <ul style="list-style-type:none;">
  23. <li><label><input type="checkbox" name="plang" class="check_all" value="PHP">PHP</label></li>
  24. <li><label><input type="checkbox" name="plang" class="check_all" value="Java">Java</label></li>
  25. <li><label><input type="checkbox" name="plang" class="check_all" value="C++">C++</label></li>
  26. <li><label><input type="checkbox" name="plang" class="check_all" value="Python">Python</label></li>
  27. </ul>
  29. <button type="button" class="btn btn-primary" onclick="selectedCheck();">Submit</button>
  30. </div>
  31. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will toggle all the checkboxes when the checkbox is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function check(source){
  2. var checkboxes = document.getElementsByClassName('check_all');
  3. for(var i in checkboxes){
  4. checkboxes[i].checked = source.checked;
  5. }
  6. }
  8. function selectedCheck(){
  9. var selected = document.getElementsByName('plang');
  11. var lang= [];
  13. for(var i in selected){
  14. if(selected[i].checked){
  15. lang.push(selected[i].value);
  16. }
  17. }
  19. var list=lang.join(', ');
  22. alert("You selected "+list+"");
  23. }
There you have it we successfully created a Check All Checkbox With One Click using 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!

Add new comment