2

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

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
  • possible duplicate of [set select option 'selected', by value](http://stackoverflow.com/questions/13343566/set-select-option-selected-by-value) – Colin Brock Nov 26 '14 at 05:47

3 Answers3

1

how about trying

$( "#output" ).text(ww);
roullie
  • 2,830
  • 16
  • 26
0

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

vengatesh rkv
  • 327
  • 2
  • 16
0

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>
PeterKA
  • 24,158
  • 5
  • 26
  • 48