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.

2
  • U all didn't get my point what I wnat Please got through question ave edited it Commented Jun 11, 2015 at 5: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 Commented Jun 11, 2015 at 7:16

4 Answers 4

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");
Sign up to request clarification or add additional context in comments.

Comments

0
$("#youSelect").val("thevalue");

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

Comments

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

5 Comments

U didn't understand what I want is $('#youSelec option:selected').text($('#myselect option:selected').text()) but its not working
so you want to set the second Option's value based on the text in the first?
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
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
Updated the answer. So you want two Options to have matched text, though they have different values
0

Use this.

$(selector_to_your_button).on('click',function(){
    $('#youSelect).val($("#myselect").val());
});

1 Comment

I think the question is pretty clear, the value of youSelect should bet set on click of a button , not on change of myselect.

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.