28

how can I find all checkboxes, that are checked and not disabled?

5 Answers 5

52

Like so:

$("input[type='checkbox']:checked").not(":disabled")...

This finds fields that are inputs, with type checkbox, which are checked, and not disabled. If this does not work, you should use an attribute check:

$("input[type='checkbox']:checked").not("[disabled]")...

Or, as @lonesomeday astutely pointed out, you can combine it into one selector:

$("input[type='checkbox']:checked:not(:disabled)")...

I've put together a proof-of-concept in this fiddle.

1
  • 7
    There's no point in using two separate selectors here. You may as well do input[type="checkbox"]:checked:not([disabled]). Commented Sep 10, 2012 at 19:39
7
$('input[type="checkbox"]').filter(function() {
return !this.disabled && this.checked;
})
6
  • 2
    Yes, but losing :checked from the selector and making the filter function !this.disabled && this.checked would improve performance a great deal. Commented Sep 10, 2012 at 19:37
  • why exactly? I would think that the selector engine would do it faster than my function. Nevertheless it is easier to understand, I will edit my answer.
    – Hoffmann
    Commented Sep 10, 2012 at 19:38
  • Because browsers don't understand :checked, so jQuery has to do it itself, which is slow. Adding an extra boolean check is much quicker. Commented Sep 10, 2012 at 19:42
  • @lonesomeday :checked is a normal CSS selector, not a jQuery extension.
    – Esailija
    Commented Sep 10, 2012 at 19:43
  • @Esailija Indeed, my mistake. Still, it didn't work in IE<9, so there's still a speed improvement in that browser. Commented Sep 10, 2012 at 19:45
7
$('input[type="checkbox"]:checked').not(":disabled");

Here's a fiddle

1
  • you have to use the .filter() function like in my response
    – Hoffmann
    Commented Sep 10, 2012 at 19:37
6

You can use this selector..

​$('input[type=checkbox]:checked:not(:disabled)')​

Check This FIDDLE

1

how about $("input[type='checkbox']:checked:enabled") ?

Not the answer you're looking for? Browse other questions tagged or ask your own question.