How to remove elements from a listbox in Javascript (IE and Firefox)

//By Gubatron, just a silly function to clear the contents of a listbox object
var ua = navigator.userAgent.toLowerCase();
var Browser = new Object()
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (ua.indexOf("firefox")!=-1);

function clearListbox(listboxObject) {	
	if (Browser.isFirefox) {
	    //in firefox "Option" objects remove themselves.
            var options = listboxObject.options
	    while (options.length > 0) {
		    options[options.length-1].remove()
	    }
	} else {
		//in IE the Options array, which contains "Option" objects, is the one in charge of removal
		while (listboxObject.options.length > 0) {
			listboxObject.options.remove(0)
		}		
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.