19

I want to set the value of a hidden field, using JQuery.

Hidden Field:

<input id="chag_sort" type="hidden" name="chag_sort">

My JQuery:

 $("#input[name=chag_sort]").val(sort2);

What am I doing wrong? I should also mention in console that sort2 does in fact have a value: DESC.

JZ.
  • 21,147
  • 32
  • 115
  • 192

3 Answers3

35

The selector should not be #input. That means a field with id="input" which is not your case. You want:

$('#chag_sort').val(sort2);

Or if your hidden input didn't have an unique id but only a name="chag_sort":

$('input[name="chag_sort"]').val(sort2);
Neil
  • 24,551
  • 15
  • 60
  • 81
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Or, for my completionist tendencies, `$('input[name="chag_sort"]').val(sort2);`. Depending on the *type* of that `input` it might be necessary to use`$('input[name="chag_sort"]').eq(0).val(sort2);` to differentiate between the possibly-various elements sharing that name. – David Thomas May 13 '11 at 22:31
3

Drop the hash - that's for identifying the id attribute.

planetjones
  • 12,469
  • 5
  • 50
  • 51
2

If you have a hidden field like this

  <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("VertragNr") %>'/>

Now you can use your value like this

$(this).parent().find('input[type=hidden]').val()

Sajidur Rahman
  • 613
  • 6
  • 9