Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onchange insert value into hidden input jquery

When i select some data from my option i need the value to be showed when "onchange" the select... can someone help me ?

<select name="search_school" id="search_school" onchange="$('#search_school').val() = $('#school_name').val()">

I want the selected option value to be showed in the hidden input

<input type="hidden" name="school_name" id="school_name" value="" />
like image 576
william Avatar asked Aug 24 '09 18:08

william


2 Answers

I think you want this as your onchange event:

<select name="search_school" id="search_school" onchange="$('#school_name').val($('#search_school').val())">

When you call val() without a parameter, it fetches the value of the element. If you call it with a parameter like val('some value');, it sets the value of the element.

like image 95
zombat Avatar answered Oct 19 '22 11:10

zombat


If you can, avoid inline event definition on html:

<select name="search_school" id="search_school">
...
</select>
<input type="hidden" name="school_name" id="school_name" />

$(document).ready(function () {
    $('#search_school').change(function () {
        $('#school_name').val($(this).val());
    });
});
like image 35
Christian C. Salvadó Avatar answered Oct 19 '22 11:10

Christian C. Salvadó