473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Delete all items from Drop Down?

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

Jul 23 '05 #1
7 19639
In article <0x*********************@news-text.cableinet.net>, a@abc.com
enlightened us with...
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

Jul 23 '05 #2
harry wrote:
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.

--
Matt Kruse
Javascript Toolbox: http://www.JavascriptToolbox.com/
Jul 23 '05 #3
In article <ca*********@news1.newsguy.com>, ne********@mattkruse.com
enlightened us with...
harry wrote:
I want to remove all items in a drop down list, surely this is
possible in one operation?


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.

--
--
~kaeli~
When a clock is hungry, it goes back four seconds.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
kaeli wrote:
selectObj.options.length=0

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.

--
Matt Kruse
Javascript Toolbox: http://www.JavascriptToolbox.com/
Jul 23 '05 #5
"harry" <a@abc.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
JRS: In article <MP************************@nntp.lucent.com>, seen in
news:comp.lang.javascript, kaeli <ti******@NOSPAM.comcast.net> posted at
Wed, 16 Jun 2004 12:48:19 :
In article <ca*********@news1.newsguy.com>, ne********@mattkruse.com
enlightened us with...
harry wrote:
> I want to remove all items in a drop down list, surely this is
> possible in one operation?


selectObj.options.length=0


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/.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
Matt Kruse wrote:
kaeli wrote:
selectObj.options.length=0

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.


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[i] 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[i], anArray[i]);
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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
2
by: Bill Manring | last post by:
In an ASP.NET web application, I am using the standard list box server control and I am adding items to with client side script. When the page posts back, the added items are not available on the...
0
by: Alienz | last post by:
hiya! I wanted to make a multiple selection drop down list and I found the easiest way to do this was by creating a subform linked to a table with 2 values, the 1st value links to the main form...
2
by: Rich | last post by:
All of my efforts to add items to display in the combobox fail; I've added items using the IDE in VS2003 (String Collection Editor) or programtically and all I get is an empty drop down list that...
2
by: Samantha Penhale | last post by:
Hi, I have a drop down list ddlServers and in my data access layer I run a stored procedure to populate it. I need to be able to append items to the drop down list and don't know how to do this....
1
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in...
5
by: mc | last post by:
I've got three controls on a page a Drop Down List, a List Box and a Button I've added 3 items to the Drop Down List I have added some code in the Button onClick Event Handler, as below...
3
by: UJ | last post by:
Is there a way to have items in a drop down list where certain items are a different color? How about having items that are strikethrough? TIA - Jeff.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.