Changing Select Element Content (two Combobox) : Option Select ComboBox « Form Control « JavaScript DHTML






Changing Select Element Content (two Combobox)

   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->
<html>
<head>
<title>Recipe 8.13</title>
<style rel="stylesheet" id="mainStyle" type="text/css">
html {background-color:#cccccc}
body {background-color:#eeeeee; font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:12px;
    margin-left:15%; margin-right:15%; border:3px groove darkred; padding:15px}
h1 {text-align:right; font-size:1.5em; font-weight:bold}
h2 {text-align:left; font-size:1.1em; font-weight:bold; text-decoration:underline}
.buttons {margin-top:10px}

</style>
<script type="text/javascript">

var regiondb = new Object()
regiondb["africa"] = [{value:"102", text:"Cairo"},
                      {value:"88", text:"Lagos"},
                      {value:"80", text:"Nairobi"},
                      {value:"55", text:"Pretoria"}];
regiondb["asia"] = [{value:"30", text:"Ankara"},
                    {value:"21", text:"Bangkok"},
                    {value:"49", text:"Beijing"},
                    {value:"76", text:"New Delhi"},
                    {value:"14", text:"Tokyo"}];
regiondb["australia"] = [{value:"64", text:"Suva"},
                          {value:"12", text:"Sydney"}];
regiondb["europe"] = [{value:"11", text:"Athens"},
                      {value:"35", text:"Frankfurt"},
                      {value:"3", text:"London"},
                      {value:"15", text:"Madrid"},
                      {value:"1", text:"Paris"},
                      {value:"10", text:"Rome"},
                      {value:"6", text:"Stockholm"},
                      {value:"97", text:"St. Petersburg"}];
regiondb["noamer"] = [{value:"73", text:"Dallas"},
                      {value:"71", text:"Los Angeles"},
                      {value:"5", text:"New York"},
                      {value:"37", text:"Toronto"}];
regiondb["soamer"] = [{value:"65", text:"Buenos Aires"},
                      {value:"31", text:"Caracas"},
                      {value:"66", text:"Rio di Janeiro"}];

function setCities(chooser) {
    var newElem;
    var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
    var cityChooser = chooser.form.elements["city"];
    while (cityChooser.options.length) {
        cityChooser.remove(0);
    }
    var choice = chooser.options[chooser.selectedIndex].value;
    var db = regiondb[choice];
    newElem = document.createElement("option");
    newElem.text = "Choose a City:";
    newElem.value = "";
    cityChooser.add(newElem, where);
    if (choice != "") {
        for (var i = 0; i < db.length; i++) {
            newElem = document.createElement("option");
            newElem.text = db[i].text;
            newElem.value = db[i].value;
            cityChooser.add(newElem, where);
        }
    }
}

/**********
  DOM LEVEL 0 ALTERNATE
 **********
function setCities(chooser) {
    var cityChooser = chooser.form.elements["city"];
    // empty previous settings
    cityChooser.options.length = 0;
    // get chosen value to act as index to regiondb hash table
    var choice = chooser.options[chooser.selectedIndex].value;
    var db = regiondb[choice];
    // insert default first item
    cityChooser.options[0] = new Option("Choose a City:", "", true, false);
    if (choice != "") {
        // loop through array of the hash table entry, and populate options
        for (var i = 0; i < db.length; i++) {
            cityChooser.options[i + 1] = new Option(db[i].text, db[i].value);
        }
    }
}
**********/
</script>
</head>
<body>
<h1>Changing Select Element Content</h1>
<hr /> 
<form name="dealers" ...>
...
Submit Request to: <select name="continent" onchange="setCities(this)">
    <option value="" selected>Choose a Region:</option>
    <option value="africa">Africa</option>
    <option value="asia">Asia</option>
    <option value="australia">Australia/Pacific</option>
    <option value="europe">Europe</option>
    <option value="noamer">North America</option>
    <option value="soamer">South America</option>
</select>&nbsp;
<select name="city">
    <option value="" selected>Choose a City:</option>
</select>
...
</form>
</body>
</html>


           
         
    
    
  








Related examples in the same category

1.Set select to multiple selected
2.Options in select
3.Option label
4.Option default Selected Example
5.Option selected Index Example
6.Option selected
7.Is Select a select-multiple
8.'index' Example
9.'value' Example
10.Add an option to select
11.'text' Example
12.Option selected index
13.Auto Linked Option Listbox without button
14.Triggering a Value Change from a Pop-Up Menu
15.Cycling through a Multiple-Selection List
16.Modifying optgroup Element Labels
17.Grouped selection control on change event
18.Change background
19.Drop-down Redirect - No Submit button
20.Drop-down Redirect - Submit
21.Dropdown list (combobox) in a form
22.dropdown list (combobox) in form 2
23.Disable or enable an option
24.Disable and enable a dropdown list (combobox)
25.Return the name of the form that contains dropdown list (combobox)
26.Number of options in the dropdown list(Combobox)
27.Change the size of a dropdown list (ComboBox)
28.Select multiple options in a dropdown list (option)
29.Return the selected option as text in option
30.Return the selected option as a number
31.Change the option text
32.Remove an option from a dropdown list (combobox)
33.Methods and Properties of the Select Object
34.Properties of the Option Object
35. Using the Location object to change another frame's URL
36.Using a Summary Form to Support Local Processing
37.Navigating with a SELECT Object
38.Using the selectedIndex Property
39. Modifying OPTGROUP Element Labels
40.Using the options[index].text Property
41. Using the options[index].value Property
42.Triggering a Color Change from a Pop-Up Menu
43.Modifying SELECT Options
44.Modifying SELECT Options (IE4+)
45.Modifying SELECT Options (NN6+)
46.Selecting an Option Using JavaScript
47.Linked ComboBox (option) Country =- province
48.ComboBox set value to TextField
49.Menu(Option) Generator
50.URL Option ComboBox
51.Show Selected Option items
52.Click button to navigate to selected url from form select control
53.Set the select control value
54.Add or remove options from select form control
55.Get changed selection from form select
56.Demonstrates select object used as a multi-line list box