Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery disable checkbox on class

I have the following code that sits within $( document ).ready(function():

$('.Spec').prop('disabled', !$('.Spec').is(':checked'));

What I am trying to do is when the page has loaded, disable any checkbox with the class Spec that are not checked.

Any reason why the above doesn't work?

like image 552
Homer_J Avatar asked Mar 12 '26 07:03

Homer_J


1 Answers

To begin with, you're doing a very poorly thought-out selector mechanism for your boolean condition; how is it going to know WHICH particular checkbox within the Spec class is the one you need to evaluate?

I might suggest something more along these lines:

$('.Spec').each(function() {
    if(!$(this).prop('checked')) {
        $(this).prop('disabled', true);
    }
});
like image 165
Vector Gorgoth Avatar answered Mar 14 '26 23:03

Vector Gorgoth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!