kumar
kumar

Reputation: 2944

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?

Upvotes: 40

Views: 138323

Answers (5)

Omotayo Kuye
Omotayo Kuye

Reputation: 1

If all the other codes do not work, try $("#element_id").selectpicker('val', '');

Upvotes: 0

Prabhjyot Saini
Prabhjyot Saini

Reputation: 21

If you want to reset bootstrap page with button click using jQuery :

function resetForm(){
        var validator = $( "#form_ID" ).validate();
        validator.resetForm();
}

Using above code you also have change the field colour as red to normal.

If you want to reset only fielded value then :

$("#form_ID")[0].reset();

Upvotes: 0

user113716
user113716

Reputation: 322502

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

Upvotes: 43

Russ Cam
Russ Cam

Reputation: 125488

If you want to reset the selected options

$('select option:selected').removeAttr('selected');

If you actually want to remove the options (although I don't think you mean this).

$('select').empty();

Substitute select for the most appropriate selector in your case (this may be by id or by CSS class). Using as is will reset all <select> elements on the page

Upvotes: 20

jAndy
jAndy

Reputation: 236022

$('#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.

Upvotes: 62

Related Questions