roni hassan
roni hassan

Reputation: 31

set selected value of a select input to another selects option value

I want to set selected value of a select input to another selects option value. when i select a value from first select box, then i want to put that value in to another select box value. but i cant get this value in another select box. i have tried this

html

<body>
    <select>
        <option id="output"></option>
    </select>

    <select id="sel">
        <option>--select--</option>
        <option>amr</option>
        <option>tomar</option>
    </select>
</body>

jquery

$(document).ready(function(){
    $( "#sel" ).change(function() {
        var ww = $( "#sel" ).val();
        alert(ww);
        $( "#output" ).val(ww);
    });
});

demo

Upvotes: 2

Views: 1805

Answers (3)

PeterKA
PeterKA

Reputation: 24638

There's no need to set an ID on an option element. Set the ID on the parent instead and use the following to set both the value and text properties of the option element. The text is what is visible while value is what is submitted to the server when a form is submitted.

$( "#output option:first" ).val(ww).text(ww);

$(document).ready(function(){
    $( "#sel" ).change(function() {
        var ww = $( "#sel" ).val();
        //alert(ww);
        $( "#output option:first" ).val(ww).text(ww);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <select id="output">
        <option></option>
    </select>

    <select id="sel">
        <option>--select--</option>
        <option>amr</option>
        <option>tomar</option>
    </select>

Upvotes: 0

vengatesh rkv
vengatesh rkv

Reputation: 327

Try this code:

HTML

<body>
<select>
<option id="output"></option>
</select>

<select id="sel">
<option>--select--</option>
<option>amr</option>
<option>tomar</option>
</select>
</body>

js:

$(document).ready(function(){
    $( "#sel" ).change(function() {

var ww = $( "#sel" ).val();
                alert(ww);
        $( "#output" ).text(ww);
});
});

For reference Demo

Upvotes: 0

roullie
roullie

Reputation: 2820

how about trying

$( "#output" ).text(ww);

Upvotes: 1

Related Questions