Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 908
Andrew Cooper Avatar asked Jun 21 '11 01:06

Andrew Cooper


2 Answers

At some point I have worked around IE's dismal performance in this area by enclosing list object in a div and then when I needed to reset it I would just set innerHTML of that div to a brand new empty list html tag. I'm sketchy on the details but I think that's what I did and it worked.
Please don't tell anybody I suggested this to you.

like image 72
MK. Avatar answered Oct 27 '22 20:10

MK.


One way that should be faster is to create a new select box with the same properties (but no options, of course), and replace the existing box with it.

like image 26
cHao Avatar answered Oct 27 '22 21:10

cHao