Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function after a certain number of checkboxes are checked

I am trying to fade a div out once a certain amount of check boxes are checked but I can't get it to work. I tried val and length but I believe I am not pulling the value the correct way for checkboxes?

$('input:checkbox').change(
    function(){
        if ($(this).is(':checked').val > 2) {
            alert('checked');
        }
    });

Any input would be greatly appreciate. Thanks!

like image 499
Myoji Avatar asked Dec 25 '22 05:12

Myoji


1 Answers

You can use a selector and check the number of elements found (length indicates this).

$('input:checkbox').change(function() {
    var numberChecked = $('input:checkbox:checked').length;
    if (numberChecked > 2) {
        // Fade your div out here
    }
});
like image 140
colti Avatar answered Dec 27 '22 17:12

colti