Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


How to select all checkboxes between two checkboxes in jQuery

Share with WhatsApp

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")) {

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.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

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.

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!