3

I have an input field that looks like this:

<input type="hidden" name="submitted[event_email]" value="">

I was trying to change the value of it and tried this:

jQuery('submitted[event_email]').val('test');

I would like to target it by the name, but i am not sure what i did wrong. Would anyone be able to help?

1
  • Your selector is targeting an element of type 'submitted' with attribute event_email setted, you need to target name attribute Commented Mar 13, 2014 at 14:15

2 Answers 2

7

You need to use attribute equals selector [name="value"]

Also from the docs:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\

So you can do:

jQuery('input[name="submitted\\[event_email\\]"]').val('test');

But actually you don't need to escape the [ ] here since it's wrapping in quotes:

jQuery('[name="submitted[event_email]"]').val('test');
Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh yes... escaping the [ ] threw me off. I knew it was something stupid. thank you!
You don't need any escaping here because string is wrapped inside quotes. I mean, this will work too: jQuery('input[name="submitted[event_email]"]').val('test'); See: jsfiddle.net/pn4Zs
3

You need to to escape [ and ] using \ but \ is also special character so we escape it also.

so we escape using \\

jQuery('[name="submitted\\[event_email\\]"]').val('test');

Attribute Equals Selector [name="value"]


Update

Fiddle Demo

There is no need to escape [ and ] as it is wrapped in quotes (i.e It is string)

jQuery('[name="submitted[event_email]"]').val('test');
//            ^                      ^

1 Comment

No, no need to escape anything here

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.