Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the dropdownlist values on button click event using jQuery?

How do I clear the dropdownlist values on button click event using jQuery?

like image 773
kumar Avatar asked Jun 06 '10 17:06

kumar


People also ask

How do you delete dropdown value?

You can clear the selected item in the below two different ways. By clicking on the clear icon which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction. By using showClearButton property, you can enable the clear icon in DropDownList element.

How do I remove the selected value of DropDownList in HTML?

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

How remove all options from dropdown in jQuery?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method.

How do you remove all options from select in jQuery except first?

Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').


2 Answers

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

That will remove all <option> elements underneath the dropdown element.

If you want to unselect selected items, go with the code from Russ.

like image 183
jAndy Avatar answered Sep 19 '22 20:09

jAndy


A shorter alternative to the first solution given by Russ Cam would be:

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

This assumes you want to retain the list, but make it so that no option is selected.

If you wish to select a particular default value, just pass that value instead of an empty string.

$('#mySelect').val('someDefaultValue'); 

or to do it by the index of the option, you could do:

$('#mySelect option:eq(0)').attr('selected','selected'); // Select first option 
like image 37
user113716 Avatar answered Sep 16 '22 20:09

user113716