0

If I've got a jQuery object that contains input fields like so:

var $inputs = $("#signup input");

Is there a way I can directly select one out of it where the name attribute equals a given value?

I've tried $inputs.find("name=firstName").select();, but it seems not to be working.

4 Answers 4

2

You need to use attribute-value selector here:

$("#signup input[name=firstName]");

update:

$inputs.filter("[name=firstname]");

Working Demo

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

5 Comments

I would like to find it in the $inputs object if that's possible.
Then where is the $inputs variable being used in your code?
I don't think that's right, either. You're only selecting the outer element of the object if it has the correct name attribute.
@isherwood: well that is what he wants.
No, we're looking for elements inside the object.
1

You were close.

$inputs.filter("[name=firstName]").select();

Or, using my preferred syntax...

$inputs.filter('[name="firstName"]').select();

Demo

http://api.jquery.com/attribute-equals-selector/

3 Comments

I've tried those, and the input is still not being selected. I don't think it finds it in the $inputs.
Hmm, in your fiddle your $inputs var contains the whole form object and not only the inputs.
Answer updated with filter() instead of find() to include top-level elements. Milind had it more correct initially than I did.
1

Use:

$inputs.filter('[name="firstName"]');

Special Note:

.select() is used to select the text in the input but not to select the way you mean.

3 Comments

It does select it in the way I mean. :P
It selects the text of it, which was my intention.
Then I guess I read my own meaning into Is there a way I can directly select one out of it where the name attribute equals a given value?. My bad! Peace :)
0

Try

var $inputs = $("#signup");
$("[name=firstName]", $inputs).select();

jsfiddle http://jsfiddle.net/guest271314/571ckuu0/

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.