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();

?

andynormancx
  • 13,421
  • 6
  • 36
  • 52
Thomas
  • 5,030
  • 20
  • 67
  • 100

4 Answers4

2

Your almost there

var name_val = $('input[name=my_element]').val();
Gary Green
  • 22,045
  • 6
  • 49
  • 75
2
var name_val = $('input[name="my_element"]').val();
netbrain
  • 9,194
  • 6
  • 42
  • 68
0

The following should work:

var name_val = $('input[name="my_element"]').val();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820