Clearing a select box
Deep in to coursework now, but I just spent more time than I care to mention struggling with what should have been a very simple task; removing all of the items from an HTML select box using Javascript. Here’s the code that was causing me problems:
endSelect = document.getElementById('endSelect');
/* Clear out the current options */
for (i = 0; i < endSelect.options.length; i++) {
endSelect.options[i] = null;
}
For some reason, this was only removing approximately half of the items in the list. I checked it in both IE and Phoenix and the same bug was evident, demonstrating that it was almost certainly a flaw in my code rather than a strange browser bug. After much tweaking and adding of alert() debugging statements I realised my folly: Every time the loop went round I was removing an item from the list of items in the select box, but my code was not taking this in to account (a classic example of changing a data stucture while processing it). Once this had dawned on me the replacement code took a matter of moments:
while (endSelect.options.length > 0) {
endSelect.options[0] = null;
}
Hopefully this will soon be Googled and will eventually save someone else from making the same stupid mistake.
Daniel Nolan - 13th December 2002 22:15 - #
Stuart Langridge - 13th December 2002 22:27 - #
Simon Willison - 13th December 2002 23:45 - #
Daniel Nolan - 13th December 2002 23:54 - #
kyle - 16th December 2002 15:47 - #
Joe Grossberg - 18th December 2002 18:34 - #
Hadley - 22nd December 2002 14:58 - #
Karun - 17th September 2003 20:02 - #
Vamsi - 27th October 2003 15:36 - #
Laurent - 30th October 2003 14:48 - #
aLX - 3rd November 2003 13:51 - #
Muhammad Asif Rahim - 10th November 2003 07:42 - #
pranesh - 10th November 2003 19:38 - #
ViV - 5th December 2003 01:46 - #
Erik - 17th January 2004 17:45 - #
Richard Leroy - 21st January 2004 18:20 - #
Leonardo - 6th February 2004 18:12 - #
Noel - 5th April 2004 03:39 - #
"This one will be better : for (var i = endSelect.options.length - 1 ; i >= 0 ; i--) { endSelect.options[i] = null; } Regards, Laurent - 30th October 2003 14:48"
Excellent Tip. Worked very well for me. - IainIain - 21st April 2004 20:33 - #
Ben - 19th May 2004 21:59 - #
Steve - 21st May 2004 02:01 - #
tim - 14th October 2004 05:05 - #
Daniel - 12th April 2005 01:27 - #
rajat - 28th April 2005 07:36 - #
Vijay - 15th June 2005 15:24 - #
Patrick Fitzgerald - 15th June 2005 15:56 - #
angel - 15th September 2005 08:14 - #
I also used
endSelect.innerHTML = ""It had some strange side effects that only occur under certain circumstances.
If the select has
endSelect.style.display="none"after usingendSelect.innerHTML = ""it would suddenly appear.It works but after a while ??? it fails. It is not really allowed i think.
Under some circumstances it will make IE fail catastrofically. :)
If you used this for clearing a select better use a different solution!!!!
i tested this
endSelect.options.length = 0and that one works great!...... until now. :)
Thanks Stuart!
JeanJean - 3rd November 2005 16:25 - #
Jeff - 8th November 2005 17:12 - #
endSelect = document.getElementById('endSelect'); /* Clear out the selected options */ for (i = 0; i < endSelect.options.length; i++) { if (endSelect.options[i].selected) { endSelect.options[i] = null; i--; // Next item moved to position 'i', move one step back so we don't skip it } }ShadowLord - 19th January 2006 10:40 - #
John Cox - 6th March 2006 23:23 - #
Ketan - 24th March 2006 09:21 - #
Peter Minne - 13th April 2006 18:02 - #
Bill - 6th July 2006 18:36 - #
morgan packard - 21st July 2006 16:46 - #
dasd - 27th July 2006 14:53 - #
try this...
var endSelect = document.getElementById("myselect"); while (endSelect.length > 0) { endSelect.remove(0); }Sam Stepanyan - 1st August 2006 15:42 - #
As per: Patrick Fitzgerald - 15th June 2005 15:56
I find this very slow indeed on IE (about 25 seconds for 800 entries!) and fine on FireFox (maybe be how IE stores some temporary data...). I have tried both length=0 and while....remove(0)
Is there perhaps a way to replace the list rather than remove all options(which IE must be doing iteratively)?
David Moore - 6th August 2006 15:54 - #
krishna - 5th October 2006 12:00 - #