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
  • 12,901
  • 6
  • 35
  • 52
Thomas
  • 4,810
  • 20
  • 65
  • 98

4 Answers4

2

Your almost there

var name_val = $('input[name=my_element]').val();
Gary Green
  • 21,457
  • 6
  • 48
  • 75
2
var name_val = $('input[name="my_element"]').val();
netbrain
  • 8,994
  • 6
  • 39
  • 67
0

The following should work:

var name_val = $('input[name="my_element"]').val();
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
0

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

Aaron Digulla
  • 310,263
  • 103
  • 579
  • 794