Noname
Noname

Reputation: 61

Changing the default selected value in dropdownlist

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

Answers (2)

ShankarSangoli
ShankarSangoli

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

Indranil
Indranil

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

Related Questions