what is the easiest way to remove all entries from a select dropdown using jquery?
i have a dropdown and i want to clear all items from it using jquery. i see a lot of googl开发者_StackOverflow社区e 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?
BEST way: use .empty()
$('select').empty();
DEMO
Note: Use .remove()
when you want to remove the element itself, as well as everything inside it
$('#idOfDropdown option').remove();
JSFiddle Example
$('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);
}
精彩评论