DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jeffrey has posted 20 posts at DZone. View Full User Profile

Remove Option Elements From A Select Element Using JavaScript

06.19.2009
| 5332 views |
  • submit to reddit
        Pretty simple, but here it is anyway.

// The DOM way.
var sel = document.getElementById('id_of_select');
for (i = sel.length - 1; i >= 0; i--) {
	sel.remove(i);
}

// The following code removes all options from a select element, but it only works 
// in Firefox, IE and Opera. It does not work in Chrome or Safari.
sel.length = 0;