Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Change value of hidden input field

I'm having a hella time setting the value of a hidden input.

I want to pass the HTML from between the option tags to the hidden field- end run it will the page title from wordpress' wp_list_dropdowns(). I've got it returning the text just fine, and on my change event it correctly adds some css (obviously unneeded on a hidden field, but I was trying to determine where things are breaking down). Works if I change the hidden input to a text input. I've seen in several places on SO that this is possible, (changing the value of a hidden input that is), but something is holding me up now and I can't see the answer.

Here's the JSFiddle:

JavaScript:

$(".selector").change(function() {     var $value = $(this).val();     var $title = $(this).children('option[value='+$value+']').html();     alert($title);      $('input#bacon').val($title).css('border','3px solid blue'); }); 

HTML:

<select class="selector" name="testselect">     <option value="1">One</option>     <option value="2">Two</option>     <option value="3">Three</option> </select> </h3>  <input id="bacon" type="hidden" class="bacon" value="" name="testinput"> 
like image 690
helgatheviking Avatar asked Mar 13 '11 12:03

helgatheviking


People also ask

How do I change the value of a hidden field?

In jQuery to set a hidden field value, we use . val() method. The jQuery . val() method is used to get or set the values of form elements such as input, select, textarea.

How to get value of input type hidden using jQuery?

#1 jQuery Code To Get hidden field value by IDvar getValue= $("#myInputHidden"). val(); alert(getValue); Here we select the input hidden field by the ID, i.e myInputHidden, and with the jQuery . val() method we get the value of it.

How to add hidden input in jQuery?

Inserting the <input type='hidden'> can also be done using jQuery. The append() and appendTo() inbuilt function can be used to add the hidden form element but they are not only limited to the <input type='hidden'> but we can also add other html elements.


2 Answers

It's simple as:

$('#action').val("1"); 

#action is hidden input field id.

like image 166
Yasir Aslam Avatar answered Sep 20 '22 12:09

Yasir Aslam


Your jQuery code works perfectly. The hidden field is being updated.

like image 39
alex Avatar answered Sep 20 '22 12:09

alex