Delete all items from Drop Down?

H

harry

I want to remove all items in a drop down list, surely this is possible in
one operation?

All I can find is to set every option in the list = null
 
K

kaeli

I want to remove all items in a drop down list, surely this is possible in
one operation?

All I can find is to set every option in the list = null


Well, not exactly all in one operation...

This works in newer browsers only (IE5+, NN6+, Mozilla, Opera, etc). If
you have to support older browsers (NN4), I believe you're stuck with
setting them to null.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function removeChildren(s)
{
while (s.hasChildNodes())
s.removeChild(s.childNodes[0]);
}
</script>
</head>

<body>
<form name="f1">
<select name="s1">
<option value="0">-- choose one --</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<input type="button" value="remove options" onClick="removeChildren
(this.form.elements['s1']);">
</form>
</body>
</html>

--
--
~kaeli~
God was my co-pilot... but then we crashed in the mountains
and I had to eat him.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Matt Kruse

harry said:
I want to remove all items in a drop down list, surely this is
possible in one operation?

selectObj.options.length=0

You'll probably want to wrap that with some sanity-checks to make sure the
select has options, etc.
 
K

kaeli

selectObj.options.length=0

I'm curious if this method actually destroys the option object, from a
memory management POV...
Do you know?

It'd be a great way, if it does.

--
 
M

Matt Kruse

kaeli said:
I'm curious if this method actually destroys the option object, from a
memory management POV...
Do you know?

I'm not sure if there is any way to verify it, but I would suspect that the
options are garbage collected since they no longer have any references to
them.
 
L

Lasse Reichstein Nielsen

harry said:
I want to remove all items in a drop down list, surely this is possible in
one operation?

Try:
document.forms['formId'].elements['selectName'].length = 0;

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
I'm curious if this method actually destroys the option object, from a
memory management POV...

See what happens if, after a little while, the length is set back to
what it used to be. If the options re-appear, they cannot have been
destroyed. But not /vice versa/.
 
G

Grant Wagner

Matt said:
I'm not sure if there is any way to verify it, but I would suspect that the
options are garbage collected since they no longer have any references to
them.

The Option objects associated with the collection would not be "destroyed", but
they will become eligible for garbage collection because there are no further
references pointing to them. Any implementation that does not remove each
<select>.options reference to it's associated Option object is in error, and
that would be the source of a "memory leak".

Note that you can cause your own "memory leaks" through sloppy coding
practices. If outside of any function you did something like:

var myOption;
for (var i = 0; i < anArray.length; i++) {
myOption = new Option(anArray, anArray);
mySelect.options[mySelect.length] = myOption;
}
mySelect.options.length = 0;

At this point, myOption still contains a reference to the last Option object
you created, so it will never be garbage collected, even though the the length
of the select of which it's a member has been set to zero. If the above code is
in a method then "myOption" becomes eligible for garbage collection once the
method exits.

In the real world, I doubt the resourced used by one (or even 100) Option
objects is significant enough to worry about.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top