how can I find all checkboxes, that are checked and not disabled?
5 Answers
Like so:
$("input[type='checkbox']:checked").not(":disabled")...
This finds fields that are input
s, 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.
-
7There'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
$('input[type="checkbox"]').filter(function() {
return !this.disabled && this.checked;
})
-
2Yes, 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.– HoffmannCommented 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.– EsailijaCommented 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