Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Value of a Hidden field using JQuery

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.

like image 679
JZ. Avatar asked May 13 '11 22:05

JZ.


People also ask

What is hidden field in jQuery?

As jQuery is a powerful javascript library by using which we can manipulate HTML DOM easily. In jQuery to get hidden field value we use . val() method. The . val() method is used to get the values of form elements such as input, select, textarea.


1 Answers

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);
like image 70
Darin Dimitrov Avatar answered Sep 19 '22 05:09

Darin Dimitrov