473,573 Members | 2,883 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 13551
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
6168
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
4655
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
11590
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...
1
10118
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
7387
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...
1
4008
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
2261
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
4384
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...
3
1966
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...
0
7784
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...
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8074
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...
1
5601
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
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...
1
2223
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
1
1310
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
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...

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.