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

5

40

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

Martinamartindale answered 6/6, 2010 at 17:14 Comment(1)
Do you want to reset them to the default (top) value?Aviate
M
43

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
Mazur answered 6/6, 2010 at 17:23 Comment(1)
this is not correct. he wants to clear all options. val(''); doesnt do that, it simpyl clears the selection. you should use .empty() insteadPosticous
D
62
$('#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.

Denoting answered 6/6, 2010 at 17:19 Comment(3)
How can i get all the options back after setting the dropdown to empty, note that the dropdown is being populated from database?Aigrette
@Piyush: If you want to restore the exact same option, you can just cache those. Like, var $opts = $('select option').detach(); and then you can re-insert those later with $opts.appendTo('#selectID');Denoting
I get how that clears it, but in my case the selection options were populated from a database based on selection of another dropdown. So, how do I put the default text there, since the default text was there from pre-ajaxing so to speak so the source shows it but the ajax doesnt. Just putting $('#blarg').val('Default Text'); didnt work for me.Duester
M
43

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
Mazur answered 6/6, 2010 at 17:23 Comment(1)
this is not correct. he wants to clear all options. val(''); doesnt do that, it simpyl clears the selection. you should use .empty() insteadPosticous
A
20

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

Aviate answered 6/6, 2010 at 17:19 Comment(1)
only this one works for me, the other answer above does NOT works.Portion
H
0

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();
Height answered 29/12, 2016 at 9:46 Comment(0)
L
0

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

Lamas answered 25/10, 2022 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.