Matt
Matt

Reputation: 5670

Disabling checkboxes that are not checked with jQuery?

How do I disable ALL checkboxes on a page that are not checked? Do I need to first scan through all the checkboxes or is there a simpler way? I'm still a jQuery newbie

Upvotes: 1

Views: 1983

Answers (3)

gen_Eric
gen_Eric

Reputation: 227240

Use checkbox to get all checkboxes, and then use :not(:checked) to filter that to the ones that aren't checked (:not docs, :checked docs).

$(':checkbox:not(:checked)').attr('disabled', 'disabled');

Upvotes: 3

ShankarSangoli
ShankarSangoli

Reputation: 69905

This will find all the checkboxes which are not checked.

$(':checkbox:not(:checked)');

Now you can either use prop or attr method to set the disabled property on the matched checkboxes.

$(':checkbox:not(:checked)').prop('disabled', true);

Upvotes: 3

Matt
Matt

Reputation: 75317

Obviously a user won't be able to ever select them again, as you've disabled them...

$(':checkbox:not(:checked)').prop('disabled', true);

See the :checkbox, :not, and :checked selector, as well as the prop() method.

If you're not using jQuery 1.6.x, then you won't have the prop() method; so use the attr() method instead:

$(':checkbox:not(:checked)').attr('disabled', true);

You can see this working in a jsfiddle that was posted in the comments: http://jsfiddle.net/loktar/LRL2k/1/

Upvotes: 9

Related Questions