26

How can I select a named input like this properly in jQuery without assigning ID's?

<input name="person[first]" type="hidden">
<input name="person[last]" type="hidden">

// Seems to be acting on multiple hidden elements instead of this named one
// This sets the value of both of those hidden elements in some cases
$("input[type=hidden][name=person[first]]").val('Test');

// So I've changed it to this. Is there a better way?
$("input[name=person[first]]").val('Test');
1
  • Can you give an example of what's not working when you have multiple hidden input elements with the HTML included? It's not clear from your example. Commented Feb 12, 2012 at 20:06

2 Answers 2

50

Put the attribute value in quotes...

$("input[type=hidden][name='person[first]']").val();
   // ---------------------^-------------^

From the docs for attribute-equals-selector...

attribute An attribute name.

value An attribute value. Can be either an unquoted single word or a quoted string.

Since you have more than just a single word, it should be quoted.

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

Comments

3

I'm pretty sure you're supposed to put in quotes any attribute selector's value that includes symbols with meaning in CSS. Like this:

input[type=hidden][name='person[first]']

Comments

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.