Jquery select all if not disabled

Viewed 40880

I am using the following script to select all checkboxes with a given class.

$(document).ready(function(){ // 1
    // 2
    $(':checkbox.selectall').on('click', function(){
        // 3
        $(':checkbox[class='+ $(this).data('checkbox-name') + ']').prop("checked", $(this).prop("checked"));
        $(':checkbox[class='+ $(this).data('checkbox-name') + ']').trigger("change");
    });

});

However I'm having a problem as the de/select all checkbox is able to de/select checkboxes which are disabled.

I tried this

$(document).ready(function(){ // 1
    // 2
    $(':checkbox.selectall').on('click', function(){
        // 3
        $(':checkbox[class='+ $(this).data('checkbox-name') + !$(:disabled) + ']').prop("checked", $(this).prop("checked"));
        $(':checkbox[class='+ $(this).data('checkbox-name') + !$(:disabled) + ']').trigger("change");
    });

});

But it does not work. I have made a jsfiddle to showcase the problem http://jsfiddle.net/e67Fv/

5 Answers

jQuery supports :enabled since v1.0

$('input:checkbox:enabled')

is the same as

$('input:checkbox:not(:disabled)')

and helps avoid the :not() selector.

Related