- Posted On 5 November 2014
- By
- In Programming
This post covers information about how to select all checkboxes between two selected checkboxes using simple jQuery code.
In one of my project there was a requirement that on list of some products if users select selects one checkbox at 4th row and again select checkbox at 8th row then in this case all checkboxes from row number 4 and 8 needs to selected.
To achieve this functionality I have added a class named "chkSelect" for each checkbox to identify it as group in jQuery code.
Code is easy to understand just by reading still lets understand code step by step for beginners
$("#chkSelectAll").bind("change", function () { $(".chkSelect").prop("checked", $(this).is(":checked")); });
In above code, I have binded a function to change event of select all checkbox which has code to check or uncheck all checkboxes as per the selected state of select all checkbox.
$(".chkSelect").bind("change", function (event, index) { if (!$(this).is(":checked")) { $("#chkSelectAll").prop("checked", false); } if ($(this).is(":checked")) { DoSelection($('.chkSelect').index($(this))); } });
Now in above code, I have binded a function to change event of checkbox having a class "chkSelect" i.e. this function will get called for a change in each checkbox of any row as we have added this class to all checkboxes.
At start I have unchcked the select all checkbix if any of checkbox gets unchecked and then have sent index of currently selected checkbox to the function "DoSelection".
function DoSelection(currentIndex) { try { if ($('.chkSelect:checked').length > 1) { var startIndex = $('.chkSelect').index($('.chkSelect:checked')[0]); if (startIndex != undefined && startIndex != null && startIndex != -1 && currentIndex > startIndex) { $("#chkSelectAll").prop("checked", false); for (var indx = startIndex; indx <= currentIndex; indx++) { $('.chkSelect').eq(indx).prop("checked", true); } } } } catch (e) { $("#chkSelectAll").prop("checked", false); $(".chkSelect").prop("checked", false); } }
In the above main function, I have actual code selection, first I have retrieved the index of first selected checkbox and then there is loop from start index and current index to select all checkboxes between them. If you observed the code I have also added a check for current index must be greater than start index and other validations.
That's it. Now if you run the code and try to select checkboxes you will find the mentioned behaviour.
Check out live fiddle of above code and don't forget to share this information with your friends by clicking on any of the share button provided. Thanks.
- Tags :
- ASP.NET
- MVC
- jQueryJavascript
How to select all checkboxes as per range of numbers in jQuery MVC
JQuery code to select all checkboxes as per range provided with select all and deselect all functionality which can be embedded in ASP.NET MVC too. With validations such as invalid range.
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.
Visual Studio 2008 Shell and TFS integration
Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
How to call click or any event only once in jQuery
Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.