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.
Probably, not the cleanest solution, but it is definitely simpler than removing one-by-one:
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:
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.
Today I was facing same problem, I did as below while reloading select box. (In Plain JS)
Hope, this code will helps you
The simplest solutions are the best, so You just need:
Above answer's code need a slight change to remove the list complete, please check this piece of code.
refresh the length and it will remove all the data from drop down list. Hope this will help someone.