473,770 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clear all optgroups and options from a select list

I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.form1. itemcross.lengt h = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?

Thanks
BrianD

Aug 4 '06 #1
16 13614
Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.form1. itemcross.lengt h = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?

Thanks
BrianD
You need an id for the object or a reference to it:

<script>
// delete an object by reference
function del(element){el ement.parentNod e.removeChild(e lement);}
// Get a reference to an object by id:
function $(id){return document.getEle mentById(id);}
// Delete an object by id:
function $del(id){x=$(id ); del(x);}
</script>

hope this helps

---
http://darwinist.googlepages.com/htmldesktop.html
(lots of working examples)

Aug 5 '06 #2

Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.form1. itemcross.lengt h = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?
The usual way:

var sel = document.form1. itemcross;
while (sel.firstChild ) {
sel.removeChild (sel.firstChild );
}
--
Rob

Aug 5 '06 #3

darwinist wrote:
Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.form1. itemcross.lengt h = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?

Thanks
BrianD

You need an id for the object or a reference to it:
The OP has already indicated how he's doing that, and may be using
either an ID or a NAME attribute.

<script>
Please don't recommend using invalid HTML. Do it in your own library
if you wish, but don't encourage it here.

// delete an object by reference
function del(element){el ement.parentNod e.removeChild(e lement);}
The OP is attempting to remove the child nodes, not the element itself.
--
Rob

Aug 5 '06 #4

RobG wrote:
darwinist wrote:
Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by
>
document.form1. itemcross.lengt h = 0;
>
The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?
>
Thanks
BrianD
You need an id for the object or a reference to it:

The OP has already indicated how he's doing that, and may be using
either an ID or a NAME attribute.

<script>

Please don't recommend using invalid HTML. Do it in your own library
if you wish, but don't encourage it here.

// delete an object by reference
function del(element){el ement.parentNod e.removeChild(e lement);}

The OP is attempting to remove the child nodes, not the element itself.
Isn't an optgroup an element that you can remove like any other?
http://www.w3schools.com/tags/tag_optgroup.asp
>
--
Rob
Aug 6 '06 #5
darwinist wrote:
RobG wrote:
>darwinist wrote:
>>Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.for m1.itemcross.le ngth = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?
[...]
>>You need an id for the object or a reference to it:
The OP has already indicated how he's doing that, and may be using
either an ID or a NAME attribute.
[...]
>> // delete an object by reference
function del(element){el ement.parentNod e.removeChild(e lement);}
The OP is attempting to remove the child nodes, not the element itself.

Isn't an optgroup an element that you can remove like any other?
Yes. Your response was essentially to give every option an ID, then
remove them one by one using getElementById. That is not a reasonable
method given the question.
--
Rob
Aug 6 '06 #6
RobG wrote:
darwinist wrote:
RobG wrote:
darwinist wrote:

Brian D wrote:
I have a multiple select list that is created dynamically based on a
previous selection on an asp page. The first thing I do is to clear
the curent option list by

document.form 1.itemcross.len gth = 0;

The only problem is that it leaves the optgroups. How do I also get
rid of the optgroups?

[...]
>You need an id for the object or a reference to it:
The OP has already indicated how he's doing that, and may be using
either an ID or a NAME attribute.

[...]
> // delete an object by reference
function del(element){el ement.parentNod e.removeChild(e lement);}
The OP is attempting to remove the child nodes, not the element itself.
Isn't an optgroup an element that you can remove like any other?

Yes. Your response was essentially to give every option an ID, then
remove them one by one using getElementById. That is not a reasonable
method given the question.
I said "or a reference to it", and gave commented, working examples of
how to deal with both. What's your problem?

Javascript's native methods use a lot of codespace for common things
that don't take much time. This rigid structure is important to the
integrity of the platform but when you are putting it to any actual
purpose you need short, clear, purpose-specific functions that reflect
what your application, not the language, is doing.

For example:
http://darwinist.googlepages.com/htmldesktop.html

Feel free to criticise or contribute.
>
--
Rob
Aug 6 '06 #7
darwinist wrote:
RobG wrote:
darwinist wrote:
RobG wrote:
>darwinist wrote:
>>
>>Brian D wrote:
>>>I have a multiple select list that is created dynamically based on a
>>>previous selection on an asp page. The first thing I do is to clear
>>>the curent option list by
>>>>
>>>document.for m1.itemcross.le ngth = 0;
>>>>
>>>The only problem is that it leaves the optgroups. How do I also get
>>>rid of the optgroups?
[...]
>>You need an id for the object or a reference to it:
>The OP has already indicated how he's doing that, and may be using
>either an ID or a NAME attribute.
[...]
>> // delete an object by reference
>> function del(element){el ement.parentNod e.removeChild(e lement);}
>The OP is attempting to remove the child nodes, not the element itself.
>
Isn't an optgroup an element that you can remove like any other?
Yes. Your response was essentially to give every option an ID, then
remove them one by one using getElementById. That is not a reasonable
method given the question.

I said "or a reference to it", and gave commented, working examples of
how to deal with both. What's your problem?
The OP already had a reference to the select element and just wanted to
remove all the child nodes. To use your proposed solution, the OP
would have looped through all the child nodes, then called the 'del'
function which used the child node to reference back to the parent node
to delete itself.

That may have lead to a few characters less in the for loop, but also
an extra unnecessary function object plus an extra couple of loop-ups
for parent and child nodes. So appart from obfuscation, you also make
the whole process less efficient.

The function I posted was perhaps 3 lines of code and could (had the OP
wanted) be wrapped in a separate 'deleteAllChild Nodes' function. I
think it actually required fewer keystrokes, not that it matters.

Incidentally, the fastest way I've seen to delete all the child nodes
of an element is to replace it with a shallow clone of itself.
Unfortunately, a few scarce browsers don't like doing that with all
elements so it's not useful on the web. But for an intranet... ;-)

Javascript's native methods use a lot of codespace for common things
that don't take much time. This rigid structure is important to the
integrity of the platform but when you are putting it to any actual
purpose you need short, clear, purpose-specific functions that reflect
what your application, not the language, is doing.
I don't see how adding an ID to every element you want to delete makes
life easier. It also suggests managing all those IDs and some
algorithm to work out which ones are of interest.

Creating single-line functions purely for the sake of reducing the
number of keystrokes for a programmer to type a method will not lead to
any great advantage in reducing software development times (that
discussion is being hosted in another thread I think). If it did, such
widely used environments as VB wouldn't have names that approach the
length of short sentences (please don't assume I think VB is some
paragon of programming excellence - it's just an example) and brevity
would be a fundamental principle of coding standards everywhere. It
isn't - clarity is.

If you've ever tried to maintain some one else's code (say C or C++)
you'd know why.
--
Rob

Aug 7 '06 #8
darwinist wrote:
RobG wrote:
>darwinist wrote:
>>>darwinist wrote:
<snip>
>>>>You need an id for the object or a reference to it:
<snip>
>>Isn't an optgroup an element that you can remove like
any other?

Yes. Your response was essentially to give every option
an ID, then remove them one by one using getElementById.
That is not a reasonable method given the question.

I said "or a reference to it", and gave commented, working
examples of how to deal with both. What's your problem?
The example you gave was an example that required each optgroup element
to have and ID, that would be the wrong thing to do.

<snip>
For example:
http://darwinist.googlepages.com/htmldesktop.html

Feel free to criticise
You have never actually said what this thing is supposed to be for. It
looks like it is indented to be the bases for an in-browser windowing
system for web-applications. It doesn't look capable enough for any
actual example of such, but I suppose could be extended for specific
applications. However, as it is only really suited for Mozilla/Gecko
browsers as it stands I don't see it being of much practical benefit in
a world where IE support is normally expected (and sometimes
sufficient).
or contribute.
You don't appear to be someone who takes advice, so anything approaching
collaboration is out of the question.

Richard.
Aug 7 '06 #9
RobG wrote:
darwinist wrote:
RobG wrote:
darwinist wrote:
RobG wrote:
darwinist wrote:
>
>Brian D wrote:
>>I have a multiple select list that is created dynamically based on a
>>previous selection on an asp page. The first thing I do is to clear
>>the curent option list by
>>>
>>document.form 1.itemcross.len gth = 0;
>>>
>>The only problem is that it leaves the optgroups. How do I also get
>>rid of the optgroups?
>
[...]
>
>You need an id for the object or a reference to it:
The OP has already indicated how he's doing that, and may be using
either an ID or a NAME attribute.
>
[...]
>
> // delete an object by reference
> function del(element){el ement.parentNod e.removeChild(e lement);}
The OP is attempting to remove the child nodes, not the element itself.

Isn't an optgroup an element that you can remove like any other?
>
Yes. Your response was essentially to give every option an ID, then
remove them one by one using getElementById. That is not a reasonable
method given the question.
I said "or a reference to it", and gave commented, working examples of
how to deal with both. What's your problem?

The OP already had a reference to the select element and just wanted to
remove all the child nodes. To use your proposed solution, the OP
would have looped through all the child nodes, then called the 'del'
function which used the child node to reference back to the parent node
to delete itself.

That may have lead to a few characters less in the for loop, but also
an extra unnecessary function object plus an extra couple of loop-ups
for parent and child nodes. So appart from obfuscation, you also make
the whole process less efficient.

The function I posted was perhaps 3 lines of code and could (had the OP
wanted) be wrapped in a separate 'deleteAllChild Nodes' function. I
think it actually required fewer keystrokes, not that it matters.
You're right to empty an object it's more efficient to have a function
that does just that, instead of a generic delete one and another loop
for every object you want to empty.
Incidentally, the fastest way I've seen to delete all the child nodes
of an element is to replace it with a shallow clone of itself.
Unfortunately, a few scarce browsers don't like doing that with all
elements so it's not useful on the web. But for an intranet... ;-)

Javascript's native methods use a lot of codespace for common things
that don't take much time. This rigid structure is important to the
integrity of the platform but when you are putting it to any actual
purpose you need short, clear, purpose-specific functions that reflect
what your application, not the language, is doing.

I don't see how adding an ID to every element you want to delete makes
life easier. It also suggests managing all those IDs and some
algorithm to work out which ones are of interest.
That was an argument for short single-word functions that have meaning
in the contenxt of a partiuclar application. The overuse of the ID tag
is just my ignorance of javascript and not related in any way.
Creating single-line functions purely for the sake of reducing the
number of keystrokes for a programmer to type a method will not lead to
any great advantage in reducing software development times (that
discussion is being hosted in another thread I think). If it did, such
widely used environments as VB wouldn't have names that approach the
length of short sentences (please don't assume I think VB is some
paragon of programming excellence - it's just an example) and brevity
would be a fundamental principle of coding standards everywhere. It
isn't - clarity is.
Brevity is not neutral with regards to clarity. Mashing a lot of words
together with hungarian notation isn't necessarily more clear than
single word sentences, but it is more precise for a framework or
toolkit. Using lots of convoluted code structures and peforming too
many operations in a single statement is also not clear, although it
may be brief.

Complete words, individual words, are what we read best, and how we
think.
If you've ever tried to maintain some one else's code (say C or C++)
you'd know why.
Funny you should mention it I've been doing precisely that for the last
few months.
>
--
Rob
Aug 7 '06 #10

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

Similar topics

1
6179
by: Adrian | last post by:
This script works well for searching thru a SELECT list but doesnt work so well with the MULTIPLE option. I need to modify it to work for a SELECT MULTIPLE but I have no idea where to begin, any tips? <script> var searchString = ""; function clearSearchString() {
4
4673
by: Pasquale | last post by:
Hello, I wondering if there is a way to dynamically update a select list with javascript from a database query without having to reload the page to update the list?
8
11616
by: asd | last post by:
I need to find the value/index of the previously selected item of a select list. That is, when the user selects an item from the list and a certain condition elsewhere in the form is not met, I need to display an alert box warning the user that a selection cannot be made, and redisplay the item that was previously selected. What is the most efficient way of doing this? Please email replies to tdk13@perfectsolve.com Thanks in advance.
1
10127
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) { combo.options.length = 0; for (i=0; i<ArrayLabel.length; i++) combo.options = new Option(ArrayLabel,ArrayValue); }
4
7404
by: bobsawyer | last post by:
I've been building a series of SELECT lists that are populated dynamically using HTTPRequest. Things are going pretty well, and I've got the whole thing working flawlessly in Mozilla/Firebird. Unfortunately, Internet Explorer doesn't quite work as expected -- it gives me an "invalid argument" error that I don't know how to fix. Here's the entire script, with form, annotated to explain what I'm doing and where the problem is occurring. I...
1
4022
by: mike | last post by:
I have a select list defined like: <select name="cri2" multiple style="height:220px; width:300px;" onClick="show_content(this);"> </select> I add content to it dynamically using a function like: function addOption(theSel, theText, theValue, optionColor) {
4
2275
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option value="2">2</option>
4
4400
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. When an option is selected in the 2nd select list, the ASP page posts itself. Assume that the 1st select list has the following 10 options (note that both the select lists are actually populated from 2 different database tables): Australia
3
1980
by: Italio Novuas | last post by:
Hi all, let me begin by saying that I *ALMOST* have this complete! What I'm trying to do is make it so my text area shows the innerHTML of any select item with the same value. For example, if I have a select list with 5 options: <select name="select" onChange="changeValues();"> <option value="1">Option 1</option> <option value="1">More stuff on option 1</option> <option value="1">EVEN more option one stuff!!</option> <option...
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10230
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10004
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.