JavaScript - How To Select All Checkboxes

In this tutorial we will create a JavaScript - How To Select All Checkboxes using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  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">JavaScript - How To Select All Checkboxes</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="form-group">
  17. <input type="checkbox" onClick="check(this)"/>
  18. <label>Select / Deselect</label>
  19. </div>
  20. <div class="col-md-6">
  21. <div style="font-size:20px;"><input type="checkbox" value="System Analyst" class="check"> <label>System Analyst</label></div>
  22. <div style="font-size:20px;"><input type="checkbox" value="Business Analyst" class="check"> <label>Business Analyst</label></div>
  23. <div style="font-size:20px;"><input type="checkbox" value="Software Engineer" class="check"> <label>Software Engineer</label></div>
  24. </div>
  25. <div class="col-md-6">
  26. <div style="font-size:20px;"><input type="checkbox" value="Technical Support" class="check"> <label>Technical Support</label></div>
  27. <div style="font-size:20px;"><input type="checkbox" value="Web Developer" class="check"> <label>Web Developer</label></div>
  28. <div style="font-size:20px;"><input type="checkbox" value="Software Tester" class="check"> <label>Software Tester</label></div>
  29. </div>
  30.  
  31. <center><button class="btn btn-primary" onclick="submit();">Submit</button></center>
  32. </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 select all the checkboxes automatically. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function selectCheckbox() {
  2. var inputs = document.getElementsByTagName('input');
  3. var checkboxes = [];
  4. for (var i = 0; i < inputs.length; i++){
  5. var input = inputs[i];
  6. if (input.getAttribute('type') == 'checkbox'){
  7. checkboxes.push(input);
  8. }
  9. }
  10. return checkboxes;
  11. }
  12.  
  13. function check(checks){
  14. var checkboxes = selectCheckbox();
  15. for(var i=0; i < checkboxes.length; i++){
  16. checkboxes[i].checked = checks.checked;
  17. }
  18. }
  19.  
  20. function submit() {
  21. var checks = document.getElementsByClassName('check');
  22. var str = '';
  23. for ( i = 0; i < checks.length; i++) {
  24. if ( checks[i].checked === true ) {
  25. str += checks[i].value + " ";
  26. }
  27. }
  28. alert(str);
  29. }
There you have it we successfully created a How To Select All Checkboxes 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