1

Is it possible to select a form element using it's name attribute?

For example if I had something like this:

<input type="text" name="my_element" />

How would I go about setting a javascript variable to the value of this input?

var name_val = $(input[name='my_element']).val();

?

4 Answers 4

2

Your almost there

var name_val = $('input[name=my_element]').val();
Sign up to request clarification or add additional context in comments.

Comments

2
var name_val = $('input[name="my_element"]').val();

Comments

0

The following should work:

var name_val = $('input[name="my_element"]').val();

Comments

0

It's even more simple: $("#form_id").elements["my_element"]

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.