How do I clear all options in a dropdown box?

JavascriptHtml Select

Javascript Problem Overview


My code works in IE but breaks in Safari, Firefox, and Opera. (big surprise)

document.getElementById("DropList").options.length=0;

After searching, I've learned that it's the length=0 that it doesn't like.
I've tried ...options=null and var clear=0; ...length=clear with the same result.

I am doing this to multiple objects at a time, so I am looking for some lightweight JS code.

Javascript Solutions


Solution 1 - Javascript

To remove the options of an HTML element of select, you can utilize the remove() method:

function removeOptions(selectElement) {
   var i, L = selectElement.options.length - 1;
   for(i = L; i >= 0; i--) {
      selectElement.remove(i);
   }
}

// using the function:
removeOptions(document.getElementById('DropList'));

It's important to remove the options backwards; as the remove() method rearranges the options collection. This way, it's guaranteed that the element to be removed still exists!

Solution 2 - Javascript

If you wish to have a lightweight script, then go for jQuery. In jQuery, the solution for removing all options will be like:

$("#droplist").empty();

Solution 3 - Javascript

Probably, not the cleanest solution, but it is definitely simpler than removing one-by-one:

document.getElementById("DropList").innerHTML = "";

Solution 4 - Javascript

This is the best way :

function (comboBox) {
    while (comboBox.options.length > 0) {                
        comboBox.remove(0);
    }        
}

Solution 5 - Javascript

You can use the following to clear all the elements.

var select = document.getElementById("DropList");
var length = select.options.length;
for (i = length-1; i >= 0; i--) {
  select.options[i] = null;
}

Solution 6 - Javascript

This is a short way:

document.getElementById('mySelect').innerText = null;

One line, no for, no JQuery, simple.

Solution 7 - Javascript

I'd like to point out that the problem in the original question is not relevant today anymore. And there is even shorter version of that solution:

selectElement.length = 0;

I've tested that both versions work in Firefox 52, Chrome 49, Opera 36, Safari 5.1, IE 11, Edge 18, latest versions of Chrome, Firefox, Samsung Internet and UC Browser on Android, Safari on iPhone 6S, Android 4.2.2 stock browser. I think it is safe to conclude that it's absolutely compatible with whatever device there is right now, so I recommend this approach.

Solution 8 - Javascript

This is a bit modern and pure JavaScript

document.querySelectorAll('#selectId option').forEach(option => option.remove())

Solution 9 - Javascript

function removeOptions(obj) {
    while (obj.options.length) {
	    obj.remove(0);
    }
}

Solution 10 - Javascript

Try

document.getElementsByTagName("Option").length=0

Or maybe look into the removeChild() function.

Or if you use jQuery framework.

$("DropList Option").each(function(){$(this).remove();});

Solution 11 - Javascript

with PrototypeJS :

$('yourSelect').select('option').invoke('remove');

Solution 12 - Javascript

Note that a select can have both

  • optgroup &
  • options collection
    as its children.

So,

Method #1

var selectElement = document.getElementById('myselectid');
selectElement.innerHTML = '';

Method #2

var selectElement = document.getElementById('myselectid');
selectElement.textContent = '';

I tested, both work on Chrome.
I like the simpler, the old fashioned, method #1.

Solution 13 - Javascript

If you are using JQuery and your select control has ID "DropList" you can remove its options doing this way:

$('#DropList option').remove();

Actually it works for me with any option list, like datalist.

Hope it helps.

Solution 14 - Javascript

Using JQuery is a prettier, shorter & smarter way to do it!

$('#selection_box_id').empty();

Solution 15 - Javascript

For Vanilla JavaScript there is simple and elegant way to do this:

for(var o of document.querySelectorAll('#DropList > option')) {
  o.remove()
}

Solution 16 - Javascript

Go reverse. Reason is size decreases after each remove.

for (i = (len-1); i > -1; i--) {
	document.getElementById("elementId").remove(i);
}

Solution 17 - Javascript

I think that is the best sol. is

 $("#myselectid").html(''); 

Solution 18 - Javascript

This can be used to clear options:

function clearDropDown(){
  var select = document.getElementById("DropList"),
      length = select.options.length;
  while(length--){
    select.remove(length);
  }
}

<select id="DropList" >
  <option>option_1</option>
  <option>option_2</option>
  <option>option_3</option>
  <option>option_4</option>
  <option>option_5</option>
</select>
<button onclick="clearDropDown()">clear list</button>

Solution 19 - Javascript

var select = document.getElementById("DropList");
var length = select.options.length;
for (i = 0; i < length; i++) {
  select.options[i].remove();
}

Hope, this code will helps you

Solution 20 - Javascript

var select = document.getElementById('/*id attribute of your select here*/');
for (var option in select){
    select.remove(option);
}

Solution 21 - Javascript

The items should be removed in reverse, otherwise it will cause an error. Also, I do not recommended simply setting the values to null, as that may cause unexpected behaviour.

var select = document.getElementById("myselect");
for (var i = select.options.length - 1 ; i >= 0 ; i--)
    select.remove(i);

Or if you prefer, you can make it a function:

function clearOptions(id)
{
    var select = document.getElementById(id);
    for (var i = select.options.length - 1 ; i >= 0 ; i--)
        select.remove(i);
}
clearOptions("myselect");

Solution 22 - Javascript

Above answer's code need a slight change to remove the list complete, please check this piece of code.

var select = document.getElementById("DropList");
var length = select.options.length;
for (i = 0; i < length;) {
  select.options[i] = null;
  length = select.options.length;
}

refresh the length and it will remove all the data from drop down list. Hope this will help someone.

Solution 23 - Javascript

The simplest solutions are the best, so You just need:

var list = document.getElementById('list');
while (list.firstChild) {
    list.removeChild(list.firstChild);
}

<select id="list">
  <option value="0">0</option>
  <option value="1">1</option>
</select>

Solution 24 - Javascript

for (var opt of document.querySelectorAll('#DropList option'))
{ 
  opt.remove();
}

This solution works with optgroups also.

Solution 25 - Javascript

while(document.getElementById("DropList").childNodes.length>0) 
{
    document.getElementById("DropList").removeChild(document.getElementById("DropList").childNodes[0]);
}

Solution 26 - Javascript

If you have to support IE and you have more than 100 items in your select list, I strongly recommend you consider replacing the select with a function like so:

function clearOptions(select) {
    var selectParentNode = select.parentNode;
    var newSelect = select.cloneNode(false); // Make a shallow copy
    selectParentNode.replaceChild(newSelect, select);
    return newSelect;
}

The select parameter should be the element either from a jquery selector or document.getElementBy call. The only downside to this is that you lose events you had wired up to the select, but you can easily reattach them as it is returned back out of the function. I was working with a select that had ~3k items and it would take 4 seconds on IE9 to clear the select so I could update it with the new content. Nearly instant doing it this way.

Solution 27 - Javascript

Today I was facing same problem, I did as below while reloading select box. (In Plain JS)

        var select = document.getElementById("item");
		select.options.length = 0;
		var opt = document.createElement('option');
		opt.value = 0;
		opt.innerHTML = "Select Item ...";
		opt.selected = "selected";
		select.appendChild(opt);


       for (var key in lands) {
			var opt = document.createElement('option');
		    opt.value = lands[key].id;
		    opt.innerHTML = lands[key].surveyNo;
		    select.appendChild(opt);
			
		}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirtView Question on Stackoverflow
Solution 1 - JavascriptFabianoView Answer on Stackoverflow
Solution 2 - JavascriptKangkanView Answer on Stackoverflow
Solution 3 - JavascriptDmitry ShintyakovView Answer on Stackoverflow
Solution 4 - JavascriptRodrigo LongoView Answer on Stackoverflow
Solution 5 - JavascriptNick CraverView Answer on Stackoverflow
Solution 6 - JavascriptFiremen26View Answer on Stackoverflow
Solution 7 - JavascriptuserView Answer on Stackoverflow
Solution 8 - Javascripta_rahmanshahView Answer on Stackoverflow
Solution 9 - JavascriptJulio GarciaView Answer on Stackoverflow
Solution 10 - JavascriptRazor StormView Answer on Stackoverflow
Solution 11 - JavascriptEpokKView Answer on Stackoverflow
Solution 12 - JavascriptManohar Reddy PoreddyView Answer on Stackoverflow
Solution 13 - JavascriptnorgematosView Answer on Stackoverflow
Solution 14 - JavascriptSobin SunnyView Answer on Stackoverflow
Solution 15 - JavascriptPiotrView Answer on Stackoverflow
Solution 16 - JavascriptDinesh KhetarpalView Answer on Stackoverflow
Solution 17 - JavascriptOssama ZakariaView Answer on Stackoverflow
Solution 18 - JavascriptVichuView Answer on Stackoverflow
Solution 19 - Javascriptuser11441779View Answer on Stackoverflow
Solution 20 - JavascriptAlex KylloView Answer on Stackoverflow
Solution 21 - JavascriptDan BrayView Answer on Stackoverflow
Solution 22 - JavascriptNoNaMeView Answer on Stackoverflow
Solution 23 - Javascriptnitka.pawelView Answer on Stackoverflow
Solution 24 - JavascriptVasilis PlavosView Answer on Stackoverflow
Solution 25 - Javascriptuser1910893View Answer on Stackoverflow
Solution 26 - JavascriptDanView Answer on Stackoverflow
Solution 27 - JavascriptChowdappaView Answer on Stackoverflow