Browse - Programming Tips - How do I remove all the options from an HTML select ?

Date: 2008may8
Language: javaScript
Keywords: clear, empty

Q.  How do I remove all the options from an HTML <select> ?

A.  Here is a function that does it.
Pass in the object handle of the <select> (not the options).

function removeOptions(obj) { if (obj == null) return; if (obj.options == null) return; obj.options.length = 0; // That's it! }
// Another way to do it: function removeOptionsAlternate(obj) { if (obj == null) return; if (obj.options == null) return; while (obj.options.length > 0) { obj.remove(0); } }
// In jQuery $('#myselect')[0].options.length = 0;
// Example use: function example() { removeOptions(document.getElementById('myselect')); }