Remove Options from a Select List with JavaScript

Removing options from a JavaScript select list

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());

Open remove-options demo

Table of Contents

Remove Selected Options Top ↑

function removeSelected(select) {
  Array.from(select.selectedOptions).forEach((option) => {
    option.remove();
  });
}

Remove All Options Top ↑

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.

Restore Options from an Array Top ↑

const values = ["One", "Two", "Three", "Four", "Five", "Six"];
function restoreOptions(select) {
  select.replaceChildren();
  values.forEach((value) => select.add(new Option(value, value)));
}

Why Older Examples Loop Backwards Top ↑

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




Subscribe to our YouTube Channel here



plus2net.com







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



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer