Muhambi
Muhambi

Reputation: 3522

Show selected option based on variable

I'm a pretty novice programmer who is trying to implement something I thought would be pretty simple, but after searching haven't found a solution.

So right now I have an variable called msg. msg is dynamically generated and can be anywhere from 2-999.

I have a select that is very simple:

<form method='post' class='myForm' action=''>
   <select name='locationSelect' class='locationSelect' data-param='location_IDNUMBER'>
        <option value='1'>Exam Room</option>
        <option value='2'>Exam Room 2</option>
        <option value='3'>X-Ray Room</option>
        <option value='1000'>Check Out</option>
   </select>
</form>

My problem is: lets say msg has a value of 3. How can I show the select with a value of 3, so the selected option(the option first visible before clicking the arrow) is X-Ray Room?

my code (taken out of a larger block of code is:

$e.find('.locationSelect').show();

How can I modify it to say something like:

$e.find('.locationSelect':value=msg).show(); //this would show the .locationSelect with the selected value being the one with the id of 3, or whatever msg is

Thanks for any and all help! If you need any more details, just ask!

Upvotes: 0

Views: 119

Answers (2)

Bruno
Bruno

Reputation: 6000

You can use $.fn.val(value) to set the selected value of a dropdown.

Note that in case you pass to $.fn.val a value that is not present in the dropdown, the first option will be selected instead.

Upvotes: 1

Musa
Musa

Reputation: 97672

Try

$e.find('.locationSelect').val(msg);

http://jsfiddle.net/mowglisanu/ktcgy/

Upvotes: 5

Related Questions