In this tutorial we will show you how to check and uncheck checkboxes using JavaScript. It helps the user to check and uncheck the checkbox and it also save user time.
You may also like Add,Edit And Delete Rows From Table Dynamically Using JavaScript
CHECK OUT THIS TUTORIAL LIVE DEMO →
To Check And Uncheck Checkboxes It Takes Only One Step:-
- Make a HTML file and define markup,scripting and styling
Step 1. Make a HTML file and define markup,scripting and styling
We make a HTML file and save it with a name checkbox.html
<html> <head> <script> function check() { var check=document.getElementsByTagName('input'); for(var i=0;i<check.length;i++) { if(check[i].type=='checkbox') { check[i].checked=true; } } } function uncheck() { var uncheck=document.getElementsByTagName('input'); for(var i=0;i<uncheck.length;i++) { if(uncheck[i].type=='checkbox') { uncheck[i].checked=false; } } } </script> <style> body { width:100%; margin:0 auto; padding:0px; background-color:#424242; font-family:helvetica; } #wrapper { text-align:center; margin:0 auto; padding:0px; width:100%; } h1 { margin-top:50px; color:#D8D8D8; } h1 p { font-size:14px; color:white; } input[type="button"] { background:none; color:white; border:1px solid white; width:100px; height:50px; border-radius:50px; margin:10px; font-weight:bold; } input[type="checkbox"] { width:20px; height:20px; } td { color:white; font-weight:bold; } </style> </head> <body> <div id="wrapper"> <input type="button" value="Check All" onclick="check();"> <input type="button" value="Uncheck All" onclick="uncheck();"> <table align='center' cellspacing='10'> <tr> <td><input type="checkbox"></td><td>PHP</td> </tr> <tr> <td><input type="checkbox"></td><td>HTML</td> </tr> <tr> <td><input type="checkbox"></td><td>JavaScript</td> </tr> <tr> <td><input type="checkbox"></td><td>jQuery</td> </tr> <tr> <td><input type="checkbox"></td><td>CSS</td> </tr> <tr> <td><input type="checkbox"></td><td>MySQL</td> </tr> </table> </div> </body> </html>
In this step we define markups for checkboxes and made two buttons to check all and uncheck all
checkbox. We create two functions check() and uncheck() to check and uncheck the checkboxes.
In first function we get all the input elements and check if type is checkbox and if yes it check the checkbox.
In second function it is same as first function only one thing is different it gets all the checkboxes and uncheck all of them. You may also like Live Character Count Using JavaScript
Thats all, this is how to check and uncheck checkboxes using JavaScript. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Latest Tutorials
- Create Animated Skill Bar Using jQuery, HTML And CSS
- Simple And Best Responsive Web Design Using CSS Part 2
- Show Button On Hover Using HTML And CSS
- Material Design Login Form Using jQuery And CSS
- Animated Progress Bar Using jQuery And CSS
- Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
- Get Website Statistics Using PHP, jQuery And MySQL
- HTML5 Login And Signup Form With Animation Using jQuery
- Facebook Style Homepage Design Using HTML And CSS
- View Webpage In Fullscreen Using jQuery