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) {
	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'));
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: