2
<select id="myselect">
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>

<select id="youSelect">
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>

Now on click of a button I want to set mySelect option:selected value in youSelect So for this I had done this

$('#youSelect option:selected').text($('#myselect option:selected').text())

but its not working.Please guide how to solve this.

Dinshaw Raje
  • 933
  • 1
  • 12
  • 33
  • U all didn't get my point what I wnat Please got through question ave edited it – Dinshaw Raje Jun 11 '15 at 05:54
  • Which jQuery version do you use? I did't get your point. Does it mean that you need to select the value in the second `dropdown` which you selected in the first `dropdown` – rrk Jun 11 '15 at 07:16

4 Answers4

1

Simply do:

$('#youSelect').val($('#myselect').val());

You are matching the values, not the text.

Edit: Based on your edited question, you're probably looking at the same situation as this question

var _mySelectText = $("#myselect option:selected").text();
$("#youSelect option").filter(function () {
    return this.text === _mySelectText;
}).attr("selected", "selected");
Community
  • 1
  • 1
Sang Suantak
  • 5,213
  • 1
  • 27
  • 46
0
$("#youSelect").val("thevalue");

just make sure the value in the options tags matches the value in the val method.

Rutunj sheladiya
  • 629
  • 7
  • 21
0
  var myValue = $("#myselect option:selected").text();
  var secondoption = $("#youSelect")[0];


  for (var i = 0, sL = secondoption.length; i < sL; i++) {
    if (secondoption.options[i].text == myValue) {
      secondoption.selectedIndex = i;
      break;
    }
  }

http://plnkr.co/edit/GVt8rZswjzZTC13vw98N?p=preview

n00b
  • 1,832
  • 14
  • 25
  • U didn't understand what I want is $('#youSelec option:selected').text($('#myselect option:selected').text()) but its not working – Dinshaw Raje Jun 11 '15 at 05:48
  • so you want to set the second Option's value based on the text in the first? – n00b Jun 11 '15 at 05:53
  • But the text from the first option does not exist as value on the second. Even if I make the plunker, your HTML is wrong – n00b Jun 11 '15 at 05:55
  • first option value in second like if I get first option:selected text as mrs so I want youSelect option:selected text is also be mrs Now u understand – Dinshaw Raje Jun 11 '15 at 05:56
  • Updated the answer. So you want two Options to have matched text, though they have different values – n00b Jun 11 '15 at 06:13
0

Use this.

$(selector_to_your_button).on('click',function(){
    $('#youSelect).val($("#myselect").val());
});
rrk
  • 15,677
  • 4
  • 29
  • 45
  • 1
    I think the question is pretty clear, the value of `youSelect` should bet set on click of a button , not on `change` of `myselect`. – Sang Suantak Jun 11 '15 at 08:48