In this tutorial, i will show you simple jquery script to check and uncheck all checkboxes on one click.
This script is very short and useful for your application. You can use it whenever you need this type of functionality.
Lets assume you have a record list and you want to perform database update or delete action then you can use this script to select all records or multiple records to do for same activity.
You ever noticed that most of the websites use this functionality to select all items on page using single select all features.
We can achieve this task by using jquery prop()
method.
The prop()
method is used to retrieve property values of the selected elements.
- <html lang="en">
- <head>
- <title>Select and deselect all checkboxes using jquery</title>
- <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
- </head>
- <body>
- <ul>
- <li><input name="product_all" class="checked_all" type="checkbox">Select All</li>
- <li><input value="1" name="product" class="checkbox" type="checkbox">Product 1 </li>
- <li> <input value="2" name="product" class="checkbox" type="checkbox">Product 2 </li>
- <li><input value="3" name="product" class="checkbox" type="checkbox">Product 3 </li>
- <li><input value="4" name="product" class="checkbox" type="checkbox">Product 4 </li>
- <li><input value="5" name="product" class="checkbox" type="checkbox">Product 5 </li>
- </ul>
- <script type="text/javascript">
- $('.checked_all').on('change', function() {
- $('.checkbox').prop('checked', $(this).prop("checked"));
- });
- //deselect "checked all", if one of the listed checkbox product is unchecked amd select "checked all" if all of the listed checkbox product is checked
- $('.checkbox').change(function(){ //".checkbox" change
- if($('.checkbox:checked').length == $('.checkbox').length){
- $('.checked_all').prop('checked',true);
- }else{
- $('.checked_all').prop('checked',false);
- }
- });
- </script>
- </body>
- </html>