Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a dropdown list in jquery so nothing is selected?

Tags:

jquery

I have this drop down list in a modal

<div class="control-group">
    <label class="control-label" for="selectInsert01">Country Name:</label>
    <div class="controls">
        <select id="selectInsert01" class="input-xlarge with-search" data-bind="foreach: citiesModel.countriesList">
            <option data-bind="text: Name, value: CountryID"></option>
        </select>
    </div>
</div>

Every time I select something and exit the modal and than open it again the selected value in the dropdown list is still selected. How do I reset the values so nothing is selected when I exit the modal

like image 528
Marko Kosanovic Avatar asked Oct 29 '13 13:10

Marko Kosanovic


People also ask

How do I remove a selected value from a DropDownList?

Select remove() Method The remove() method is used to remove an option from a drop-down list. Tip: To add an option to a drop-down list, use the add() method.

How can get default selected value of dropdown in jQuery?

val () method in jQuery: This method is used to get the values of form elements or set the value of attribute used for the selected elements. Syntax: $(selector). val ();

How do you empty a select in JavaScript?

To remove the options of an HTML element of select , you can utilize the remove() method: It's important to remove the options backwards; as the remove() method rearranges the options collection. This way, it's guaranteed that the element to be removed still exists!


3 Answers

On modal close

$("#selectInsert01 option:eq(0)").prop("selected", true); //set option of index 0 to selected
like image 171
tymeJV Avatar answered Oct 04 '22 00:10

tymeJV


This worked for me. Hope it helps.

$("#selectInsert01 option:selected").removeAttr("selected");
like image 22
You Rule Avi Avatar answered Oct 03 '22 23:10

You Rule Avi


If you have used bootstrap class selectpicker in your drop-down list:

$('.selectpicker').selectpicker('val', '-1');

You can also refer this link: http://www.jquerybyexample.net/2012/03/how-to-reset-dropdown-using-jquery.html

like image 33
Prabhjyot Saini Avatar answered Oct 03 '22 23:10

Prabhjyot Saini