jQuery: selecting checked checkbox
You should quote the attribute when selecting and include []
:
$("#myform input[name='foo[]']:checked:enabled");
Actually, what you have works just fine, just use double quotes, and include the brackets so it can match.
$("#myform input[name='foo[]']:checked:enabled");
You can see it in action here: http://jsbin.com/oyoro
Note that jQuery has had issues on and off with brackets in the past, which can explain a lot of the confusion surrounding the need for escaping. I know I've had issue myself with brackets and just quoting.
See: http://dev.jquery.com/ticket/3443
The name of the field isn't foo
, it is foo[]
. You could use the attributeStartsWith selector:
$("input[name^='foo']:checked:enabled",'#myform');
Ideally, you'd be able to do this:
$("input[name='foo[]']:checked:enabled",'#myform');
But as this answer explains, jQuery uses this to parse the value
part of the attr=value
condition:
(['"]*)(.*?)\3|)\s*\]
\3 being the group containing the opening quotes, which weirdly are allowed to be multiple opening quotes, or no opening quotes at all. The .*? then can parse any character, including quotes until it hits the first ‘]’ character, ending the match. There is no provision for backslash-escaping CSS special characters, so you can't match an arbitrary string value in jQuery.
In other words, as soon as jQuery hits the first ]
it thinks the value is over. So you are stuck with startsWith or using pure DOM elements, as that answer also explains.
SUPER DUPER IMPORTANT EDIT:
This bug is fixed, apparently. You should be able to use the code I described as "ideal", above.