37

Suppose I have the following HTML:

<form id="myform">
  <input type='checkbox' name='foo[]'/> Check 1<br/>
  <input type='checkbox' name='foo[]' checked='true'/> Check 2<br/>
  <input type='checkbox' name='foo[]'/> Check 3<br/>
</form>

Now, how do I select the checked input fields with name 'foo[]'?

This is my attempt, but it doesn't work:

$("#myform input[name='foo']:checked:enabled");
2
  • Umn, what you have there works just fine, just use double quotes around your selector. :) Commented Apr 14, 2009 at 14:06
  • 1
    Yes; be aware my initial answer is not the best way of doing this. jQuery used to have problems with brackets in the selector but this has been fixed, so you should be able to keep what you have, just use double quotes around the selector. Commented Apr 14, 2009 at 14:40

4 Answers 4

56

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe, that this is the bug and it's related fix: dev.jquery.com/ticket/3443
It's not fixed in jQuery 1.3.2, which appears to be the latest generally available version.
5

You should quote the attribute when selecting and include []:

$("#myform input[name='foo[]']:checked:enabled");

4 Comments

This won't work, unfortunately. The jQuery parser ends at the first ] it encounters, so [] aren't allowed in this case.
Indeed. This is a bug; see stackoverflow.com/questions/739695 for discussion.
An old bug. It's fixed if you're using the latest version appraently, as you can see my example shows.
My comment there was about the latest version (at the time of writing: 1.3.2). The regex used to parse attr selectors is pretty much totally broken in this version. Maybe a fix will be forthcoming later.
1

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

4 Comments

mumble mumble mumble If I'm wrong, I'll recant, but the example is there.
Touche, kind sir. -1 removed, +1. I've updated my answer with this newfound knowledge.
Sorry for the confusion, I was going by what bobince had said in an answer a few days ago so I figured it was a current thing. Will check for myself next time.
Oh, a good point. This question conversation has gotten pretty convoluted.
0

I believe it goes something like this:

$("input #foo[] :checked");

EDIT: The commenter is right. I should have gone with my first response. :) This time I am going to use the "^".

$("input[name^=foo] :checked");

2 Comments

However, the [] may be worng :)
#xyz is for ‘id’ attributes, not ‘name’.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.