Reputation: 196861
i have a dropdown and i want to clear all items from it using jquery. i see a lot of google links about removing selected item but i want to clear ALL items from a dropdown.
what is the best way of removing all items from a select dropdown list?
Upvotes: 14
Views: 16780
Reputation: 54074
BEST way: use .empty()
$('select').empty();
Note: Use .remove()
when you want to remove the element itself, as well as everything inside it
Upvotes: 41
Reputation: 82814
$('option', the_select_element).remove();
If you want to keep the selected:
$('option:not(:selected)', the_select_element).remove();
It's really simple in plain JS, too (thanks, @Austin France!):
// get the element
var sel = document.getElementById("the_select_ID");
// as long as it has options
while (sel.options.length) {
// remove the first and repeat
sel.remove(0);
}
Upvotes: 3