leora
leora

Reputation: 196539

how to "reset" a combobox using jquery

i have a select dropdown on a webpage:

<select class="designs" id="color" name="Color">
 <option value="">Select Color . . .</option>
 <option value="Dark">Dark</option>
 <option value="White">White</option>
 <option value="Light">Light</option>
</select>

and i am trying to find the right jquery code to select the first entry on that combobox (the one with option value ="" and the text of "Select Color . . ." (this is the setting that is on the page during original load before the user makes any changes)

I am doing this off of a button click link that says "Reset".

Upvotes: 5

Views: 33743

Answers (3)

Its Aafrin
Its Aafrin

Reputation: 59

You can use the .empty() function to clear the option of the combo box.

 $('#color').empty();

Upvotes: 2

Jakub Konecki
Jakub Konecki

Reputation: 46008

$('#color option:first-child').attr("selected", "selected");

$('#color')[0].selectedIndex = 0;

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

You could use the .val() function. And because the value of the element you want to preselect is empty you pass an empty string:

$('#color').val('');

Upvotes: 12

Related Questions