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?
Related Topics
JavaScript: Set and read Input Value without jQuery using pure JavaScript
Question | 1 Answer
jQuery: Note when leaving a website and a form has not been submitted
Tutorial | 2 Comments
Send form input as an array to PHP script
Tip | 0 Comments
jQuery: Count characters from multiple input fields
Tip | 0 Comments
jQuery: Toggle Checkboxes (Select/Unselect All)
Tip | 0 Comments
jQuery: Undo event.preventDefault()
Question | 2 Answers
jQuery: Check if an element is invisible
Question | 2 Answers
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.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
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