Alex Rivera | Logout

Is there a faster way to empty a HTML Select box?

Asked 2011-06-21T01:05:38.693
9

I have an HTML Select box with about 1800+ options. In my javascript I have the following line to empty the select box so I can repopulate it.

box.options.length = 0; 

In Firefox this runs fairly quickly, but IE takes a couple of seconds. Is there a faster way to do this in IE?

Edit
Report

1 Answer

5

You could use box.innerHTML="". In my test, it is 68% faster:

http://jsperf.com/emptying-a-select-box/4.

Update: in 2015 box.innerHTML = "" is by multiple orders of magnitude the slowest option. :-) Use box.options.length = 0 instead.

answered 2011-06-21T01:16:33.397

Your Answer