Hitu Bansal
Hitu Bansal

Reputation: 5

How to select a drop-down option based on a value?

I have the following drop-down:

<select id="#test">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Now I am getting a value on document.ready like:

var res = $('#someotherid').val();

which is 1,2 or 3.

Now I want this value automatically set in the drop-down.

Does anyone know how to do this?

Upvotes: 0

Views: 1637

Answers (1)

James Allardice
James Allardice

Reputation: 166041

You can set the selected option with the val method too, so after you get the value of whatever #someotherid is, you can use it to set the selected option of #test:

var res= $('#someotherid').val();
$("#test").val(res);

Here's a working example.

Upvotes: 1

Related Questions