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?
Select the cells with the drop-down list. Click Data >Data Validation. On the Settings tab, click Clear All. Click OK.
To remove all options from a select list using jQuery, the simplest way is to use the empty() method.
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)').
The detach() method removes the selected elements, including all text and child nodes.
BEST way: use .empty()
$('select').empty();
DEMO
Note: Use .remove()
when you want to remove the element itself, as well as everything inside it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With