5

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="" />

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

woow! Thanks ALOOT! I appreciate very much !
Wow. Even though I gave a right answer that's extremely simple, you guys are downvoting it because it's got a difference in semantical set up? That's pretty classy. I could have gone into a whole spiel about why you might want to use ready() and the best way to organize your javascript, but the poster obviously just needed some direction with the val() function. Sometimes keeping it simple is the best course of action. Generally you should reserve a downvote for an incorrect answer. Leave comments if you think it can be improved, or upvote other answers that are correct.
If you updated the answer to include the advice, and reasons to avoid it, Ill be back to remove the downvote.
6

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());
    });
});

Comments

0

You want something like this, I think:

$("#search_school").change(function(){
  $(this).val($("#search_school option:selected").text());
});

Comments

0

To set the selected value to your hidden control you should :

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

Comments

0
<script type="text/javascript">
    $(document).ready(function() {
        $("#search_school").change(
            function () {
                $('#school_name').attr('value', $("#search_school").val());
            }
        );
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.