Dave's Brain

Browse - programming tips - javascript remove all options in a select

Date: 2008may8
Language: javaScript

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) {
	obj.options.length = 0;
}

function removeOptionsAlternate(obj) {
	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, dave - Code on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License.