473,655 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic SELECT boxes with <OPTGROUP>s

I have two select boxes. When the user picks a value in the first one it
completely re-populates the second one. It works fine, but only generates a
standard OPTIONS list and I now want to group the options using OPTGROUPs.
<select name="Selector1 " size="1"
onchange="reloa d2(this.options .selectedIndex, document.Form1. Selector2)">
<option .....
</select>

<script type="text/javascript">
function reload2(Index1, Selector2)
{
// Do not bother blanking '0', as it will always be re-written.
for (x1=Selector2.o ptions.length-1; x1>0; x1--)
{
Selector2.optio ns[x1]=null;
}

switch (Index1)
{
case 0: // Disclaimer
Selector2.optio ns[0]=new Option("Disclai mer",
"disclaimer.htm l");
break;

case 1: // Chapter 1
Selector2.optio ns[0]=new Option("Chapter 1 Title",
"chapter1.html" );
Selector2.optio ns[1]=new Option("Section 1.1",
"chapter1.html# 1.1");
Selector2.optio ns[2]=new Option("Section 1.2",
"chapter1.html# 1.2");
Selector2.optio ns[3]=new Option("Section 1.3",
"chapter1.html# 1.3");
break;

....

case 99: // Chapter 99 - Stockists
Selector2.optio ns[0]=new Option("Chapter 99 - Stockists",
"stockists.html ");
Selector2.optio ns[1]=new Option("USA - Arizona",
"stockists.html #USA-AZ");
Selector2.optio ns[2]=new Option("USA - California",
"stockists.html #USA-CA");
Selector2.optio ns[3]=new Option("USA - Colorado",
"stockists.html #USA-CO");
Selector2.optio ns[4]=new Option("USA - Texas",
"stockists.html #USA-TX");
Selector2.optio ns[5]=new Option("Belgium ", "stockists.html #be");
Selector2.optio ns[6]=new Option("France" , "stockists.html #fr");
Selector2.optio ns[7]=new Option("Germany ", "stockists.html #de");
break;

default: // Unrecognised chapter.
alert("Chapter " + x + " is not available.");
Selector2.optio ns[0]=new Option("Unknown ", "unknown.html") ;
break;
}
Selector2.optio ns[0].selected=true;
return true;
}
</script>

This works fine, and for chapter 99 it creates an options list like this:
<option selected value="stockist s.html">Chapter 99 - Stockists</option>
<option value="stockist s.html#USA-AZ">USA - Arizona</option>
<option value="stockist s.html#USA-CA">USA - California</option>
<option value="stockist s.html#USA-CO">USA - Colorado</option>
<option value="stockist s.html#USA-TX">USA - Texas</option>
<option value="stockist s.html#be">Belg ium</option>
<option value="stockist s.html#fr">Fran ce</option>
<option value="stockist s.html#ge">Germ any</option>

But as the list has grown, I want to group the items, like this:

<option selected value="stockist s.html">Chapter 99 - Stockists</option>
<optgroup label="USA">
<option value="stockist s.html#USA-AZ">Arizona</option>
<option value="stockist s.html#USA-CA">Californi a</option>
<option value="stockist s.html#USA-CO">Colorado</option>
<option value="stockist s.html#USA-TX">Texas</option>
</optgroup>
<optgroup label="Europe">
<option value="stockist s.html#be">Belg ium</option>
<option value="stockist s.html#fr">Fran ce</option>
<option value="stockist s.html#ge">Germ any</option>
</optgroup>

Does anyone know how I can do this dynamically (client-side) ?

--
ce****@waitrose .com
Jul 20 '05 #1
5 35132
cedawe wrote:
I have two select boxes. When the user picks a value in the first one it
completely re-populates the second one. It works fine, but only generates a
standard OPTIONS list and I now want to group the options using OPTGROUPs.
[...]
Does anyone know how I can do this dynamically (client-side) ?


Using the W3C-DOM, it should be easy:

// create new option group
var oGroup = document.create Element('optgro up');
oGroup.value = "...";

// create new option (maybe you can/must use the Option(...) constructor)
var oOption = document.create Element('option ');
oOption.value = "...";
oOption.text = "...";
...

// append the option to the option group
oGroup.appendCh ild(oOption);

// get a reference to the `select' element
var oSelect = document.forms[...].elements[...];

// append the option group
oSelect.appendC hild(oGroup);

Untested, HTH.
PointedEars

Jul 20 '05 #2
I realize this is 6 months after the fact but...

This should read as

var oGroup = document.create Element('OPTGRO UP');
oGroup.label = "..."; <===== Use ".label"

and ...

var oOption = document.create Element('OPTION ');
oOption.value = "...";
oOption.innerTe xt = "..."; <==== Use ".innerText "

oGroup.appendCh ild(blah!);
oSelect.appendC hile(blah!);

....blah, blah!
I have only verified this behaviour on IE6.
Did anyone verify on Opera or NN?

"Thomas 'PointedEars' Lahn" <Po*********@we b.de> wrote in message
news:bo******** *****@ID-107532.news.uni-berlin.de...
cedawe wrote:
I have two select boxes. When the user picks a value in the first one it completely re-populates the second one. It works fine, but only generates a standard OPTIONS list and I now want to group the options using OPTGROUPs. [...]
Does anyone know how I can do this dynamically (client-side) ?
Using the W3C-DOM, it should be easy:

// create new option group
var oGroup = document.create Element('optgro up');
oGroup.value = "...";

// create new option (maybe you can/must use the Option(...)

constructor) var oOption = document.create Element('option ');
oOption.value = "...";
oOption.text = "...";
...

// append the option to the option group
oGroup.appendCh ild(oOption);

// get a reference to the `select' element
var oSelect = document.forms[...].elements[...];

// append the option group
oSelect.appendC hild(oGroup);

Untested, HTH.
PointedEars

Jul 23 '05 #3
A. Nonymous wrote:
I realize this is 6 months after the fact but...
This should read as
var oGroup = document.create Element('OPTGRO UP');
oGroup.label = "..."; <===== Use ".label"
var oOption = document.create Element('OPTION ');
oOption.value = "...";
oOption.innerTe xt = "..."; <==== Use ".innerText "
oGroup.appendCh ild(blah!);
oSelect.appendC hile(blah!);
...blah, blah!
I have only verified this behaviour on IE6.
Did anyone verify on Opera or NN?


My Netscape 7.1 didn't like innerText, but it does like innerHTML, if
you change that line it works on my IE6, Netscape 7.1, Firefox 0.8.
Here's how I tested it:

<script type="text/javascript">
function populate() {
// create new option group
var oGroup = document.create Element('optgro up');
// get reference to the `select' element
var oSelect = document.getEle mentById('selec t1');
//document.forms['form1'].elements['select1'];
oGroup.label = 'group1';
for (var i=0; i<5; i++) {
// create new options
var oOption = document.create Element('option ');
oOption.value = 'b'+i;
oOption.innerHT ML = 'b'+i;
// append the option to the option group
oGroup.appendCh ild(oOption);
// append the option group
oSelect.appendC hild(oGroup);
}
}
function show(oSelect,oS electIndex){
alert('You selected '+oSelect[oSelectIndex].value);
}
</script>

<form name="form1" id="form1">
<select name="select1" id="select1" onclick="show(t his,this.select edIndex)">
<option value='a0'>a0</option>
<option value='a1'>a1</option>
<option value='a2'>a2</option>
<option value='a3'>a3</option>
<option value='a4'>a4</option>
</select>
<p>
<input type="button" onclick="popula te()" value="Add Select Options">
</form>

Mike

Jul 23 '05 #4
mscir wrote:
A. Nonymous wrote:
I realize this is 6 months after the fact but...
This should read as
var oGroup = document.create Element('OPTGRO UP');
oGroup.label = "..."; <===== Use ".label"
var oOption = document.create Element('OPTION ');
oOption.value = "...";
oOption.innerTe xt = "..."; <==== Use ".innerText "
oGroup.appendCh ild(blah!);
oSelect.appendC hile(blah!);
...blah, blah!
I have only verified this behaviour on IE6.
Did anyone verify on Opera or NN?
My Netscape 7.1 didn't like innerText, but it does like innerHTML, if
you change that line it works on my IE6, Netscape 7.1, Firefox 0.8.
Here's how I tested it:

<script type="text/javascript">
function populate() {
// create new option group
var oGroup = document.create Element('optgro up');
// get reference to the `select' element
var oSelect = document.getEle mentById('selec t1');
//document.forms['form1'].elements['select1'];
oGroup.label = 'group1';
for (var i=0; i<5; i++) {
// create new options
var oOption = document.create Element('option ');
oOption.value = 'b'+i;
oOption.innerHT ML = 'b'+i;


oOption.appendC hild(document.c reateTextNode(' b'+i));

In cases like these, where the content is not too complicated (and not likely to
become complicated), I prefer document.create TextNode() to setting .innerHTML.
Any standards compliant browser that understands document.create Element() is
more likely to understand document.create TextNode() then .innerHTML. Making this
minor change removes any dependancy on proprietary attributes from your script.

That said, if I'm attempting to display extremely complicated, dynamically
created content, I typically use .innerHTML since it is such a huge pain to
construct complicated HTML using the standard methods. It also lets me write
code that can usually work on Netscape 4, since the same string can be output
using document.write( ).
// append the option to the option group
oGroup.appendCh ild(oOption);
// append the option group
oSelect.appendC hild(oGroup);
}
}
function show(oSelect,oS electIndex){
alert('You selected '+oSelect[oSelectIndex].value);
}
</script>

<form name="form1" id="form1">
<select name="select1" id="select1" onclick="show(t his,this.select edIndex)">
<option value='a0'>a0</option>
<option value='a1'>a1</option>
<option value='a2'>a2</option>
<option value='a3'>a3</option>
<option value='a4'>a4</option>
</select>
<p>
<input type="button" onclick="popula te()" value="Add Select Options">
</form>

Mike


--
| Grant Wagner <gw*****@agrico reunited.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 #5
A. Nonymous wrote:
This should read as

var oGroup = document.create Element('OPTGRO UP');
oGroup.label = "..."; <===== Use ".label"
Correct.

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-38450247>
<http://msdn.microsoft. com/workshop/author/dhtml/reference/properties/label_1.asp>
and ...

var oOption = document.create Element('OPTION ');
oOption.value = "...";
oOption.innerTe xt = "..."; <==== Use ".innerText "
No, the proper property identifier as defined in DOM Level 0 (IE3+, NN3+) is
indeed "text". "innerText" is possible in IE, but in no way cross-browser.

<http://devedge.netscap e.com/library/manuals/2000/javascript/1.3/reference/option.html>
<http://msdn.microsoft. com/workshop/author/dhtml/reference/properties/text_3.asp>
[...]


Please do not top-post, see the FAQ.
PointedEars
Jul 23 '05 #6

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

Similar topics

4
6483
by: Ron Lounsbury | last post by:
I have a customer who has requested that I add "popup" text (a la ToolTips) to a couple of pulldown lists in a DHTML form we have in our project. He says - Just use the ALT tag. I went and double checked, and the ALT property only shows up for the IMG tag. Can anyone help me with how to implement this? What they want is some amplification to the list selections. TIA Ron Lounsbury
10
7637
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how the onchange event isn't called when mouse wheel scrolling between A3 and B1, but it works properly when scrolling between A1 and A2. E.g. ------------------------------------------
2
2018
by: VK | last post by:
A while ago there was a discussion how to implement a select list that would call a function right upon new selection is being made w/o clicking any additional buttons: <http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/aa4a8da635e42592/ba2c264d6a9b3558?q=drag+group:comp.lang.javascript+author:VK&rnum=2&hl=en#ba2c264d6a9b3558> The main issue was to overcome IE's accessibility bug: if user scrolls the list using...
1
2787
by: Stewart Cambridge London, UK | last post by:
I've been having some trouble with a set of <select><optgroup><option> dropdowns. I've been experimenting with the disabled attribute in the <optgroup> and <option> tags. Seems to work in NN but not IE. Yet this link shows Microsoft advertising that the disabled attribute is supported by them.
44
2818
by: rhythmace | last post by:
W3C HTML validator passes this: .... <script type="text/javascript" src="foo.js"> <script type="text/javascript"> ....script in here... </script> ....
2
1653
by: PW | last post by:
Is it possible to make a select list item red based on a condition ? TIA, PW
1
5070
by: madflytom | last post by:
Hello, I'm trying to move the options of one select list to another select list. The "source" select list is divided into optgroups, and the "target" select list is not. I want to somehow keep track of the moved option's optgroup, so that if I remove it from the "target" select list, it moves back into it's respective optgroup in the "source" select list. I have things moving back and forth, but I'm not sure how to handle the optgroup...
5
13348
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
1964
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main titles/headings or clicking on the 'expand all' will allow you to view the items underneath each title/heading. The page seems to be broken at the Incident and Request Management title/heading section as shown in the image attached. Can anyone help /...
0
8380
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
8296
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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
8497
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,...
1
6162
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
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
4150
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...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
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

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.