11 Vote

jQuery: Select form fields with square brackets (such as names[]) using jQuery

Question by Guest | 2012-07-18 at 19:13

I have an HTML form including some fields named with square brackets in order to access them later better with PHP. So for example:

<input type="text" name="names[]">

Now I want to use JavaScript/jQuery to select and to change these fields. However, jQuery apparently does not like the brackets, because as soon as I leave them out, it is working.

Is there still a way to select names with square brackets using jQuery?

2Best Answer4 Votes

The square brackets [ and ] have a special meaning within a selector in jQuery. Therefore, you have to escape those brackets that they are recognized as part of the name.

Here's an example:


The last bracket is the closing bracket of the first bracket. The second last bracket is part of the name of the items to be selected. So this is masked with \\ and thus evaluated as part of the name.
2012-07-19 at 22:55

ReplyPositive Negative

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.