How can i dynamically remove all options of a drop down box in javascript?
To remove all options from a select list using jQuery, the simplest way is to use the empty() method. If we want to remove all options from the select list, we can use the jQuery empty() method to do this with the following Javascript code.
The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the Reset JavaScript function is executed. Inside this function, the SelectedIndex property of the DropDownList is set to 0 (First Item).
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)').
document.getElementById('id').options.length = 0;
or
document.getElementById('id').innerHTML = "";
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