Check and Uncheck all checkbox using jQuery Example

Check and Uncheck all checkbox using jQuery Example

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.

  1. <html lang="en">
  2. <head>
  3.     <title>Select and deselect all checkboxes using jquery</title>
  4.     <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  5. </head>
  6. <body>
  7. <ul>
  8.     <li><input name="product_all" class="checked_all" type="checkbox">Select All</li>
  9.     <li><input value="1" name="product" class="checkbox" type="checkbox">Product 1 </li>
  10. <li> <input value="2" name="product" class="checkbox" type="checkbox">Product 2 </li>
  11. <li><input value="3" name="product" class="checkbox" type="checkbox">Product 3 </li>
  12. <li><input value="4" name="product" class="checkbox" type="checkbox">Product 4 </li>
  13. <li><input value="5" name="product" class="checkbox" type="checkbox">Product 5 </li>
  14. </ul>
  15. <script type="text/javascript">
  16.      $('.checked_all').on('change', function() {     
  17.              $('.checkbox').prop('checked', $(this).prop("checked"));          
  18.         });
  19.         //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
  20.         $('.checkbox').change(function(){ //".checkbox" change
  21.             if($('.checkbox:checked').length == $('.checkbox').length){
  22.          $('.checked_all').prop('checked',true);
  23.          }else{
  24.               $('.checked_all').prop('checked',false);
  25.          }
  26.         });
  27. </script>
  28. </body>
  29. </html>

Phone: (+91) 8800417876
Noida, 201301