Check and Uncheck all checkbox using jQuery Example
In this post we will give you information about Check and Uncheck all checkbox using jQuery Example. Hear we will give you detail about Check and Uncheck all checkbox using jQuery ExampleAnd how to use it also give you demo for it if it is necessary.
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>
<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" type="checkbox">Select All</li> <li><input value="1" name="product" type="checkbox">Product 1 </li> <li> <input value="2" name="product" type="checkbox">Product 2 </li> <li><input value="3" name="product" type="checkbox">Product 3 </li> <li><input value="4" name="product" type="checkbox">Product 4 </li> <li><input value="5" name="product" 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>
Hope this code and post will helped you for implement Check and Uncheck all checkbox using jQuery Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs