[Javascript] deleting all options

James Conley Conleyj at kubota-kma.com
Wed Feb 1 11:29:20 CST 2006


Oddly enough this works in IE - does it work in other browsers as well? 
Normally .length is read-only for arrays and such.
 

james c

 


________________________________

From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu] On Behalf Of Peter Brunone
Sent: Wednesday, February 01, 2006 10:53 AM
To: javascript at LaTech.edu
Subject: Re: [Javascript] deleting all options


Can you still set options.length = 0?

Also, innerHTML = "" comes to mind (this might possibly be fastest,
though not supported by earlier browsers)...

Peter

From: "Nick Fitzsimons" nick at nickfitz.co.uk

> i dynamically fill a with options depending on an ID given by>
another.
> when i do it twice the first created options still are in the new
select.
>
> now i need ans easy function to clear the of all its options.>> do i
have to count through every option of that select or is there a> faster
way?Assuming your select has an id, as in
you can use

// get reference to element
var element = document.getElementById("mySelect");
// remove all children of element
while (element.firstChild) {
element.removeChild(element.firstChild);
}

As you presumably already have a variable reference to the select,
adjust
accordingly.

The while loop there is a general purpose way of removing all the child
nodes of any element, so depending on your needs, you may find it useful
to move it into a function.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20060201/bb67ba25/attachment.htm>


More information about the Javascript mailing list