Options can be removed from a select individually, as a selected group, or all at once. For a multiple-select, selectedOptions gives the options currently selected by the user.
const list = document.getElementById("itemList");
Array.from(list.selectedOptions).forEach((option) => option.remove());
function removeSelected(select) {
Array.from(select.selectedOptions).forEach((option) => {
option.remove();
});
}
If the select should become completely empty, replaceChildren() removes all option nodes in one operation.
function removeAll(select) {
select.replaceChildren();
}
If a placeholder must remain, remove only options after the placeholder or rebuild the desired list.
const values = ["One", "Two", "Three", "Four", "Five", "Six"];
function restoreOptions(select) {
select.replaceChildren();
values.forEach((value) => select.add(new Option(value, value)));
}
The original page removed options by index from the end toward index 0. That pattern is valid when mutating a live options collection because deleting a lower index shifts later indexes. selectedOptions plus Array.from() is clearer for selected-item removal.
for (let i = select.options.length - 1; i >= 0; i--) {
if (select.options[i].selected) {
select.remove(i);
}
}
Add options · Move options · Listbox overview · JavaScript validation
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| ashutosh | 02-03-2010 |
| Thanks a lot. code is working fine in IE but not working with MOZILA. Please help me. | |
| smo | 02-03-2010 |
| Check the demo link, it is working fine in FireFox, Chrome & IE | |
| Manas Sinha | 16-05-2010 |
| Yes,It worked. Actually I used options.remove(i) so it was not removing all. Thanks... | |
| Lakshman | 28-06-2010 |
| function removeAllOptions(selectbox) { selectbox.length = 0; } | |
| Achmend | 20-09-2010 |
| I have many list boxes in one form. How do i clear all the list in all the boxes? | |
| suryaprakash | 21-01-2011 |
| hi Achmend Get The All selectBox Ids in Javascript and and their Length ,then perForm Remove Method using For loop for all | |
| adi | 04-04-2011 |
| i have a problem. i want to sign in using the username, password and one option from the drop down box, there are two option and on both optin different page is open how can i do this plz help me | |
| Srinivas | 26-03-2012 |
| I dont need remove all ,remove and buttons there.As soon as i select an item from the list it should get deleted. | |
05-01-2023 | |
| Really helpful | |