C
cedawe
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="reload2(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.options.length-1; x1>0; x1--)
{
Selector2.options[x1]=null;
}
switch (Index1)
{
case 0: // Disclaimer
Selector2.options[0]=new Option("Disclaimer",
"disclaimer.html");
break;
case 1: // Chapter 1
Selector2.options[0]=new Option("Chapter 1 Title",
"chapter1.html");
Selector2.options[1]=new Option("Section 1.1",
"chapter1.html#1.1");
Selector2.options[2]=new Option("Section 1.2",
"chapter1.html#1.2");
Selector2.options[3]=new Option("Section 1.3",
"chapter1.html#1.3");
break;
....
case 99: // Chapter 99 - Stockists
Selector2.options[0]=new Option("Chapter 99 - Stockists",
"stockists.html");
Selector2.options[1]=new Option("USA - Arizona",
"stockists.html#USA-AZ");
Selector2.options[2]=new Option("USA - California",
"stockists.html#USA-CA");
Selector2.options[3]=new Option("USA - Colorado",
"stockists.html#USA-CO");
Selector2.options[4]=new Option("USA - Texas",
"stockists.html#USA-TX");
Selector2.options[5]=new Option("Belgium", "stockists.html#be");
Selector2.options[6]=new Option("France", "stockists.html#fr");
Selector2.options[7]=new Option("Germany", "stockists.html#de");
break;
default: // Unrecognised chapter.
alert("Chapter " + x + " is not available.");
Selector2.options[0]=new Option("Unknown", "unknown.html");
break;
}
Selector2.options[0].selected=true;
return true;
}
</script>
This works fine, and for chapter 99 it creates an options list like this:
<option selected value="stockists.html">Chapter 99 - Stockists</option>
<option value="stockists.html#USA-AZ">USA - Arizona</option>
<option value="stockists.html#USA-CA">USA - California</option>
<option value="stockists.html#USA-CO">USA - Colorado</option>
<option value="stockists.html#USA-TX">USA - Texas</option>
<option value="stockists.html#be">Belgium</option>
<option value="stockists.html#fr">France</option>
<option value="stockists.html#ge">Germany</option>
But as the list has grown, I want to group the items, like this:
<option selected value="stockists.html">Chapter 99 - Stockists</option>
<optgroup label="USA">
<option value="stockists.html#USA-AZ">Arizona</option>
<option value="stockists.html#USA-CA">California</option>
<option value="stockists.html#USA-CO">Colorado</option>
<option value="stockists.html#USA-TX">Texas</option>
</optgroup>
<optgroup label="Europe">
<option value="stockists.html#be">Belgium</option>
<option value="stockists.html#fr">France</option>
<option value="stockists.html#ge">Germany</option>
</optgroup>
Does anyone know how I can do this dynamically (client-side) ?
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="reload2(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.options.length-1; x1>0; x1--)
{
Selector2.options[x1]=null;
}
switch (Index1)
{
case 0: // Disclaimer
Selector2.options[0]=new Option("Disclaimer",
"disclaimer.html");
break;
case 1: // Chapter 1
Selector2.options[0]=new Option("Chapter 1 Title",
"chapter1.html");
Selector2.options[1]=new Option("Section 1.1",
"chapter1.html#1.1");
Selector2.options[2]=new Option("Section 1.2",
"chapter1.html#1.2");
Selector2.options[3]=new Option("Section 1.3",
"chapter1.html#1.3");
break;
....
case 99: // Chapter 99 - Stockists
Selector2.options[0]=new Option("Chapter 99 - Stockists",
"stockists.html");
Selector2.options[1]=new Option("USA - Arizona",
"stockists.html#USA-AZ");
Selector2.options[2]=new Option("USA - California",
"stockists.html#USA-CA");
Selector2.options[3]=new Option("USA - Colorado",
"stockists.html#USA-CO");
Selector2.options[4]=new Option("USA - Texas",
"stockists.html#USA-TX");
Selector2.options[5]=new Option("Belgium", "stockists.html#be");
Selector2.options[6]=new Option("France", "stockists.html#fr");
Selector2.options[7]=new Option("Germany", "stockists.html#de");
break;
default: // Unrecognised chapter.
alert("Chapter " + x + " is not available.");
Selector2.options[0]=new Option("Unknown", "unknown.html");
break;
}
Selector2.options[0].selected=true;
return true;
}
</script>
This works fine, and for chapter 99 it creates an options list like this:
<option selected value="stockists.html">Chapter 99 - Stockists</option>
<option value="stockists.html#USA-AZ">USA - Arizona</option>
<option value="stockists.html#USA-CA">USA - California</option>
<option value="stockists.html#USA-CO">USA - Colorado</option>
<option value="stockists.html#USA-TX">USA - Texas</option>
<option value="stockists.html#be">Belgium</option>
<option value="stockists.html#fr">France</option>
<option value="stockists.html#ge">Germany</option>
But as the list has grown, I want to group the items, like this:
<option selected value="stockists.html">Chapter 99 - Stockists</option>
<optgroup label="USA">
<option value="stockists.html#USA-AZ">Arizona</option>
<option value="stockists.html#USA-CA">California</option>
<option value="stockists.html#USA-CO">Colorado</option>
<option value="stockists.html#USA-TX">Texas</option>
</optgroup>
<optgroup label="Europe">
<option value="stockists.html#be">Belgium</option>
<option value="stockists.html#fr">France</option>
<option value="stockists.html#ge">Germany</option>
</optgroup>
Does anyone know how I can do this dynamically (client-side) ?