Headshota
Headshota

Reputation: 21449

jquery select element by name attribute

I have an input with the name attribute:

<input type="text" name="data[foo][bar]" />

how can I select this element?

I tried $("input[name=data[foo][bar]]") but in vein.

Upvotes: 0

Views: 3858

Answers (4)

Jonas Stensved
Jonas Stensved

Reputation: 15276

Use

$("input[name=data\\[foo\\]\\[bar\\]]")

The documentation says:

If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \. For example, if you have an element with id="foo.bar", you can use the selector $("#foo\.bar").

http://api.jquery.com/category/selectors/

Upvotes: 0

Treemonkey
Treemonkey

Reputation: 2163

$('input[name="data[foo][bar]"]')

http://api.jquery.com/attribute-equals-selector/ for more info

Upvotes: 0

Vivek
Vivek

Reputation: 11028

$("input[name='data[foo][bar]']") 

Upvotes: 0

BoltClock
BoltClock

Reputation: 723448

Add quotes to the attribute value, otherwise you get conflicting square brackets and a parse error:

$("input[name='data[foo][bar]']")

Upvotes: 11

Related Questions