We help IT Professionals succeed at work.

Clearing options collection of Javascript select

ARACK04
ARACK04 asked
on
Is there a fast way to clear all items from a javascript select?  I've got about 100,000 items in there, returned from some query, and I want to simply dump its contents before the page posts back.  Saying:

$get(name).options.length = 0

took too long, and

$get(name).options = new Array();  threw an exception.  Is there some other way?
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
if you give the select list an id:
<select id="someList">...</select>
then you can simply say:
document.getElelementById("someList").options.length=0;

That will be the fastest. What does $get() do for you? I suspect it is doing an AJAX call. If anything, the AJAX call is the one that is causing the delay.

Author

Commented:
$get is part of the Microsoft AJAX JavaScript library - it's a replacement for document.getElementById

Despite being part of the "AJAX" library, it's pure javascript, and doesn't involve the XMLHttpRequestObject, so it's not causing my slowdown.

I tried setting length  = 0 - for a dropdown of about 7,000 items it took about a half a second, and the time just skyrockets as it increases to 100,000.

If this is the fastest I can get, then I guess I have to look elsewhere.  Thanks.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
Have you tried:
$get("selectId").options=null;
OR
$get("selectId").options=new Array();

Author

Commented:
yeah, tried them both, both slow.  Thanks for the help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.