I am creating an select like this:
<select id="teamNameLabel" data-corners="false" onchange="GetSelectedTeam()" data-bind="options: teams, optionsText: 'name', optionsValue: 'id'">
<option value="1">Select a Team...</option>
</select>
My application is a single page, so when an option is selected it is assigned to TeamId like this:
TeamId = $('#teamNameLabel').val();
I would like the selected option to be what was last selected when you navigate back to this page. I tested it with this when the page loaded, but the alert keeps coming back as undefined.
$("#teamNamelabel option").eq(TeamId+1).attr("selected","selected");
alert($("teamNameLabel option:selected").val());
Is there anyway i can set the selectedOption or .attr(selected) to be TeamId?
You are missing
#in that selector inside your alert and also you can justinstead of
And to set the value.. simply use
.valagain like below,You can use
.eqselector when you need to select an option without knowing its value.Assume you want to select 3rd option, then