[Javascript] deleting all options

Peter Brunone peter at brunone.com
Wed Feb 1 09:53:21 CST 2006


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/c100943c/attachment.htm>


More information about the Javascript mailing list