Dave's Brain

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); } }
// Example use: function example() { removeOptions(document.getElementById('myselect')); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.