Reputation: 61
How to change the default selected value of an drop down list. I want the default value to best seller instead of price low to high.
The code:
<select name="ddlSort" id="ddlSort" onchange="javascript:callSortOrder()" class="dropdown" gtbfieldid="2">
<option selected="selected" value="">Price low to high</option>
<option value="1">Price high to low</option>
<option value="2">Best seller</option>
<option value="3">Highest rating</option>
</select>
Upvotes: 1
Views: 8119
Reputation: 69915
Use the below markup. selected="selected"
attribute tells the browser to select that option.
<select name="ddlSort" id="ddlSort" onchange="javascript:callSortOrder()" class="dropdown" gtbfieldid="2">
<option value="">Price low to high</option>
<option value="1">Price high to low</option>
<option selected="selected" value="2">Best seller</option>
<option value="3">Highest rating</option>
</select>
If you want to do it programmatically using jQuery try this
$("#ddlSort").val('2');
Upvotes: 1
Reputation: 2471
Just put selected="selected"
on the option
you want, and remove it from the other options.
So, in your case
<select name="ddlSort" id="ddlSort" onchange="javascript:callSortOrder()" class="dropdown" gtbfieldid="2">
<option value="">Price low to high</option>
<option value="1">Price high to low</option>
<option selected="selected" value="2">Best seller</option>
<option value="3">Highest rating</option>
</select>
Upvotes: 1