473,405 Members | 2,167 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.

Clear Elements in Select Menu

Does anyone have the code to clear all of the elements in a select
menu?

I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;

because it doesn't clear the elements.

Thanks for your help.
Dustin

Jul 23 '05 #1
6 5975
Hi Dustin,

Try this on for size:

for(var i = 0; i < 100; i++)
{
document.addskill.cat2.remove(0);
}

You might ask why are we continuously removing an element from index 0.
That is because once you remove, all the other elements move down. So
you have to keep removing from index 0.

Hope this helps. :)

Jul 23 '05 #2
trope wrote:
Does anyone have the code to clear all of the elements in a select
menu?

I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;

because it doesn't clear the elements.


document.addskill.cat2.options.length=0
Mick
Jul 23 '05 #3
trope wrote:
Does anyone have the code to clear all of the elements in a select
menu?

I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;

because it doesn't clear the elements.


'clear'? You can remove all the options by setting the length of the
select to zero (no loop required):

document.addskill.cat2.length = 0;

If you want set the value or text of the option to '' then you have
to do that for each option:

for (var i=0; i< 100; i++){
document.addskill.cat2.options[i].text = '';
document.addskill.cat2.options[i].value = '';
...
}
--
Rob
Jul 23 '05 #4
web.dev wrote:
That is because once you remove, all the other elements move down. So
you have to keep removing from index 0.


Thanks alot, I spent the most part of a day on friday trying to debug a
script, simply because I did not know this!

I finally managed to get it done by looping backwards through the
options array (i--). However, your suggestion is definately more elegant..
Jul 23 '05 #5
Use the MS Script Editor included free with MS Office 2002 and above,
for debugging Internet Explorer (IE).

This subject is of great interest to many JS developers, as there is no
obvious, low cost way to do sophisticated debugging in
IE6 other than to use the debugger described below, which is horribly
documented otherwise. I feel debugging is an important aspect of
projecting the useability of the language and needs to be made more
clear for new users.
Jeff Papineau
yo**@mandala.com
<FAQENTRY>

This is a page that describes how to install and use the MS Script
Editor to debug Javascript in Internet Explorer ( IE ). It has a
powerful debugger built into it that works really well for developers
supporting IE5+. This debugger/editor included with most versions of
Microsoft Office.

http://www.mandala.com/javascript/debug_javascript.html

..NET programmers may have better tools (VStudio) but this comes in
really handy for anyone developing with JSP and PHP and other dynamic
scripting languages which embed javascript, as well as any HTML page
using Javascript in Internet Explorer that needs a (almost) free
debugging environment.

</FAQENTRY>
trope wrote:
Does anyone have the code to clear all of the elements in a select
menu?

I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;

because it doesn't clear the elements.

Thanks for your help.
Dustin


Jul 23 '05 #6
Mick White wrote:
trope wrote:
Does anyone have the code to clear all of the elements in a select
menu?

I've been having trouble with this following code:
for (var i=0; i< 100; i++)
document.addskill.cat2.options[i] = null;

because it doesn't clear the elements.


document.addskill.cat2.options.length=0


Does not work in all cases, therefore my dhtml.js, version 0.9.2005050817,
contains

/**
* Removes all options from a HTMLSelectElement object.
*
* @author
* (C) 2003 Thomas Lahn &lt;dh******@PointedEars.de&gt;
* @partof
* http://pointedears.de/scripts/dhtml.js
* @param oSelect : HTMLSelectElement
* Reference to a HTMLSelectElement object.
* @param bAllowReload : boolean
* If <code>true</code>, reloads the document.
* @return type boolean
* <code>true</code> if successful, <code>false</code>
* otherwise.
*/
function removeOptions(oSelect, bAllowReload)
{
if (oSelect
&& oSelect.tagName
&& oSelect.tagName.toLowerCase() == "select")
{
var o = oSelect.options;
if (o && o.length)
{
// shortcut if "length" property is not read-only
o.length = 0;
while (o.length > 0)
{
if (o.remove)
{
o.remove(o.length - 1);
}
else
{
o[o.length - 1] = null;
if (bAllowReload)
{
history.go(0);
}
}
}
return true;
}
}

return false;
}

Note that the remove() method suggested in
<11*********************@g49g2000cwa.googlegroups. com>
presents a proprietary approach (IE only AFAIK) which
therefore must be feature-tested before.
PointedEars
Jul 23 '05 #7

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

Similar topics

0
by: Gary Smart | last post by:
Hello. I've been searching news groups and apache and google, but can't solve this one: I have an XML file used to create menu items in a Swing UI. I have a <menu> element that can have an...
1
by: Kjell Breimo | last post by:
Hi, I have made a selectbox (<Select....><option...>...) with size=1(It has to be size=1). It contains about 40 elements. It is placed on the top of my webpage abd I would like to see as many...
1
by: SAN CAZIANO | last post by:
how can clear an html SELECT and next insert in it all the elements of an array () I try this but seems doesn't works. function ComboAddArrayValueWithLabel(combo,ArrayLabel,ArrayValue) { ...
25
by: Tim Zych | last post by:
Is there keystroke combination that will clear the command window in Visual Studio.Net? Or, is there a way for me to clear it in vb.net code, prior to my code running? Thanks.
3
by: groups2 | last post by:
I have a form with a dropdown (select) menu a text input field and some hidden values, along with an input botton that triggers an ajax function that submits the form. If the button is after the...
4
by: saritha2008 | last post by:
Hi, Iam working on converting one of xml file to other form of xml using XSLT. As part of this, I need to count the no. of "component" nodes in the xml file given below. If there is only one...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.