1

I have amount of checkboxes. For Example 100; I need to choose 20, different checkboxes. When I will check 20 ChB, I would like to disable checking any other boxes, but enable to uncheck cheked boxes. Is it real? I know IDs of all checked boxes. How can i do this by JQuery?

    function PageSelected(sender) {
    selectedIds = [];
    var enough = 0;
    $('input[type=checkbox]').each(function () {
       if (avblPagesCount <= selectedIds.length)  {
            $('#'+sender.id).attr("checked", false);
        }

       if (avblPagesCount > selectedIds.length) {
            if(this.checked) {
                selectedIds.push(this.id);                                        
            }
        }
    });       
};
Arthur
  • 1,740
  • 3
  • 16
  • 36
  • possible duplicate of [How do I check a checkbox with jQuery or JavaScript?](http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript) – Grevling Sep 24 '13 at 07:48
  • 2
    I read it. It's not duplicate – Arthur Sep 24 '13 at 07:52

1 Answers1

1

The below should work for your case.

When the user checks 3 check-boxes, the rest of the check-boxes will get disabled and user will not be able to select them. But the three check-boxes that are checked will remain enabled for un-checking purpose. If the user un-checks one of them, the others become enabled again (because the number of check-boxes that are checked will become less than 3).

$('input[type="checkbox"]').on('change', function () {
    var chcked = $('input:checked').length;
    $('input[type="checkbox"]:not(:checked)').prop('disabled', chcked == 3);
});

Working Demo

Harry
  • 87,580
  • 25
  • 202
  • 214