Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a select element with jQuery

Tags:

html

jquery

I have

<select id="baba"> <option>select something</option> <option value="1">something 1</option> <option value=2">something 2</option> </select> 

Using jQuery, what would be the easiest (less to write) way to reset the select so the first option is selected?

like image 797
Itay Moav -Malimovka Avatar asked May 08 '12 15:05

Itay Moav -Malimovka


People also ask

How do you clear a selection in HTML?

To remove the options of an HTML element of select , you can utilize the remove() method: function removeOptions(selectElement) { var i, L = selectElement.

How do you reset a drop down?

To reset a dropdown in JavaScript, set selectedIndex to 0 .

How do I remove a selected value from a DropDownList?

With jQuery, you can use the . remove() method to takes elements out of the DOM. To get the selected item from a dropdown, you can use the :selected property and call remove() on the matched element.


2 Answers

Try this. This will work. $('#baba').prop('selectedIndex',0);

Check here http://jsfiddle.net/bibin_v/R4s3U/

like image 158
Bibin Velayudhan Avatar answered Nov 06 '22 04:11

Bibin Velayudhan


In your case (and in most use cases I have seen), all you need is:

$("#baba").val(""); 

Demo.

like image 44
karim79 Avatar answered Nov 06 '22 03:11

karim79