Home > Software design >  Modify an old JQuery script to select a specific number of checkboxes
Modify an old JQuery script to select a specific number of checkboxes

Time:12-17

We have an old site that is still running in our company. It's an aspx site running jquery. There is a script is currently being use so when a person checks a checkbox called "SelectAllGroups" it will select all the checkboxes. We need to modify this so it will only select the first 11 checkboxes. We are not JQuery experts at all, this was dumped in out lap and it is only a patch until we can get the new site running. Here is the current script:

$(".rightColumn .SelectAllGroups").on("click", function (evt) {
    if (this.checked) {
        $(".GroupSelection input:checkbox").each(function () {
            this.checked = true;
        });
    } else {
        $(".GroupSelection input:checkbox").each(function () {
            this.checked = false;
        });
    }
});

I just need the section if(this.checked) {});; method modified to just select the first 11 boxes. The else should be fine as is. Normally I would try and search this out, but this is a quick patch to help the last end users fight through this system. JQuery is not in our wheelhouse and if someone has a quick script for us that would be great.

CodePudding user response:

jQuery.each passes the callback function the element's index in the collection as the first argument:

$(".GroupSelection input:checkbox").each(function (index) {
  this.checked = index < 11;
});
  • Related