tags:

views:

80

answers:

5

Hi everyone,

I need to insert the text of a selected text box into a hidden field, I'm not quite sure how to achieve that,

any help would be appriciated.

<form id="select">
                <select name="select" id="select">
                  <option>NY, 10&quot;, £6.65</option>
                  <option>NY, 12&quot;, £8.95</option>
                  <option>NY, 16&quot;, £11.95</option>
                  <option>Chicago, 7&quot;, £3.45</option>
                  <option>Chicago, 10&quot;, £6.65</option>
                  <option>Chicago, 12&quot;, £8.95</option>
                  <option>Chicago, 16&quot;, £11.95</option>
                  <option>Chicago, Beast 24&quot; x 18&quot;, £19.95</option>
                 </select>
         </form>


<form id="add-pizza">
<input type="hidden" name="my-item-name" value="" />
</form>

I need to insert the text of the text box into the hidden field value="".

   $(function()
    {
    var str="";
    $("#select option:selected").each(function() {
        str += $this.text() + "";
});

  });

thanks

A: 
$('#id_of_hidden_input').val($('#id_of_select').val());
chaos
+2  A: 

You can use the change function of the select to run a function that copies the text of the selected option into the hidden input.

Example:

$('#select').change( function(){
  $('input[name=my-item-name]').val( $(this).children(':selected').text() );
});
redsquare
A: 

Since you aren't providing a value to the select, the text of the select will also be the value.

$('#select').change( function() {
    $('input[name=my-item-name]').val( $(this).val() );
});
tvanfosson
A: 

try adding an id to your hidden field first. then i believe you can just do

$("#yourHiddenFieldID").val(str);
Jason
A: 

This library provides such functionality: http://www.texotela.co.uk/code/jquery/select/

Zed