Clearing options AND optgroups in a select
Until today, I always cleared options from a select by doing something like this:
JavaScript:
-
//Clear select of exiting options
-
element.options.length = 0;
A quick Google search turned up this blog post where they had this helpful snippet:
JavaScript:
-
//Clear select of exiting options and optgroups
-
while (element.hasChildNodes()) {
-
element.removeChild(element.firstChild);
-
}
This clears all items in the select and there is no noticeable performance loss when dealing with a select with a reasonable number of options.