user123456789
user123456789

Reputation: 566

Restricting user to check checkboxes based on minimum value using jQuery

Here i have bunch of check boxes within their respective div's.

Every div will contain a 'data-min' (ex: data-min="2") value to restrict user to check minimum no of check boxes in a div.

Demo:Fiddle

HTML

<select id="count">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>
<br/>
<br/>
<div class="checkbox" data-toggle="false" data-min="2" style='float:left;background:yellow;width:100px'>
    <input id="checkbox-1" type="checkbox" name="Data1" value="option1" />
    <label for="checkbox-1">HTML</label>
    <br />
    <input id="checkbox-2" type="checkbox" name="Data2" value="option2" />
    <label for="checkbox-2">CSS</label>
    <br />
    <input id="checkbox-3" type="checkbox" name="Data3" value="option3" />
    <label for="checkbox-3">HTML</label>
    <br />
</div>
<div class="checkbox" data-toggle="false" data-min="2" style='float:left;margin-left:100px;background:brown;width:100px'>
    <input id="checkbox-4" type="checkbox" name="Data4" value="option4" />
    <label for="checkbox-4">CSS</label>
    <br />
    <input id="checkbox-5" type="checkbox" name="Data5" value="option5" />
    <label for="checkbox-5">HTML</label>
    <br />
    <input id="checkbox-6" type="checkbox" name="Data6" value="option6" />
    <label for="checkbox-6">CSS</label>
</div>

jQuery

$(document).ready(function () {
    $('#count').on('change', function () {
        $('input[type=checkbox]').prop('checked', false);
        $('[data-toggle]').data('toggle', false);
    });
    $('input[type=checkbox]').on('change', function () {
        if ($('input[type=checkbox]:checked').length > $('#count').val()) {
            $(this).prop('checked', false);
        }
    });
    $('.checkbox').on('click', function (e) {
        var cnt = $('input[type="checkbox"]:checked').length;
        var cntSel = $('select').val();
        var fin = cntSel - cnt;

        var min = $('.checkbox').data('min');

        if (e.target.className == "checkbox") {
            if ($(this).data('toggle') == false) {
                $(this).data('toggle', true);
                $(this).find('input[type="checkbox"]:lt(' + fin + ')').prop('checked', true);
            } else {
                $(this).data('toggle', false);
                $(this).find('input[type="checkbox"]').prop('checked', false);
            }

        }
    });
});

How can i do this, can anyone help me out.

Edit1:

  1. Here in this fiddle, user can check a check boxes from both div's based on select box value.
  2. check box count should not exceed select box value.
  3. And we should consider the 'data-min' value also.

for example,

Upvotes: 1

Views: 2065

Answers (3)

user123456789
user123456789

Reputation: 566

After a lot of effort , Finally i got my answer

Fiddle

jQuery

$(document).ready(function () {
    $('#count').on('change', function () {
        //do stuff here

        //my random "on change" behaviour {
        var chkboxes = $('input[type=checkbox]:checked');
        for (var i = 0; i < chkboxes.length; i++) {
            $(chkboxes[i]).prop('checked', false);
        }
        //end of my random "on change" bahaviour}
    });

    $('input[type=checkbox]').on('change', function (e) {
        //this was deselected no need to check?
        if (!$(this).prop('checked')) {
            return false;
        }

        //checked count exceeded #count value
        if ($('input[type=checkbox]:checked').length > $('#count').val()) {
            $(this).prop('checked', false);
            alert('Count of checked checkboxes >= #count');
            return false;
        }

        //if checked check boxes count = #count, validate all divs
        if ($('input[type=checkbox]:checked').length == $('#count').val()) {
            validateDivs();
        }

        var checksLeft = parseInt($('#count').val()) - $('input[type=checkbox]:checked').length;
        var parent = $($(this).parent()[0]);
        var parentSub = parent.find('input[type=checkbox]:checked').length; //count of checked checkboxes in current parent

        if (parseInt(parent.attr('data-min')) - parentSub > checksLeft) {
            $(this).prop('checked', false);
            alert('Not enought checks left');
            return false;
        }
    });

    $('.checkbox').on('click', function (e) {
        var cnt = $('input[type="checkbox"]:checked').length;
        var cntSel = $('select').val();
        var fin = cntSel - cnt;
        var min = $(this).data('min');

        if (e.target.className == "checkbox") {
            if ($(this).data('toggle') == false) {
                $(this).data('toggle', true);
                if (fin < min) {
                    $(this).prop('checked', false);
                    alert("Minimun capacity of this table is: " + min + ".");
                } else {
                    $(this).find('input[type="checkbox"]:lt(' + fin + ')').prop('checked', true);
                }
            } else {
                $(this).data('toggle', false);
                $(this).find('input[type="checkbox"]').prop('checked', false);
            }

        }
    });
});

function validateDivs() {
    var chkboxes = $('.checkbox');
    for (var i = 0; i < chkboxes.length; i++) {
        var chks = $(chkboxes[i]).find('input[type=checkbox]:checked');
        if (chks.length < $(chkboxes[i]).attr('data-min')) {
            chks.each(function (index, checkbox) {
                $(checkbox).prop('checked', false);
            })
        }
    }
}

Thanks for everyone, for supporting me to solve this issue.

Upvotes: 2

Dart
Dart

Reputation: 787

Not complete, just 1-st thoughts http://jsfiddle.net/HNmhL/35/ NOTE: $('checkboxId').prop('checked', false) could break checkbox, don't know how, but Pavlo said this

$(document).ready(function () {
    $('#count').on('change', function(){
        //do stuff here

        //my random "on change" behaviour {
        var chkboxes = $('input[type=checkbox]:checked');
        for(var i = 0; i < chkboxes.length; i++){
            $(chkboxes[i]).prop('checked', false);
        }
        //end of my random "on change" bahaviour}
    });

    $('input[type=checkbox]').on('change', function(e){
        //this was deselected no need to check?
        if(!$(this).prop('checked')){
            return false;
        }

        //checked count exceeded #count value
        if($('input[type=checkbox]:checked').length > $('#count').val()){
            $(this).prop('checked', false);
            alert('Count of checked checkboxes >= #count');
            return false;
        }

        //if checked check boxes count = #count, validate all divs
        if($('input[type=checkbox]:checked').length == $('#count').val()){
            validateDivs();
        }        

        var checksLeft = parseInt($('#count').val()) - $('input[type=checkbox]:checked').length;
        var parent     = $($(this).parent()[0]);
        var parentSub  = parent.find('input[type=checkbox]:checked').length; //count of checked checkboxes in current parent

        if(parseInt(parent.attr('data-min')) - parentSub > checksLeft){
            $(this).prop('checked', false);
            alert('Not enought checks left');
            return false;
        }
    });
});

function validateDivs(){
    var chkboxes = $('.checkbox');
    for(var i = 0; i < chkboxes.length; i++){
        var chks = $(chkboxes[i]).find('input[type=checkbox]:checked');
        if(chks.length < $(chkboxes[i]).attr('data-min')){
            chks.each(function(index, checkbox){
                $(checkbox).prop('checked', false);
            })
        }
    }
}

Upvotes: 1

Sanjeevi Rajagopalan
Sanjeevi Rajagopalan

Reputation: 232

Did i not answer a similar question of yours??? Anyways check out this fiddle example.

$('input[type=checkbox]').on('click', function(event){
    if($('div.checkbox input[type=checkbox]:checked').length > $('#count').val())
    {
        event.preventDefault();
    }
});

Upvotes: 0

Related Questions