<!--
Dynamic drop downs using Javascript
Copyright (C) 2008 sos aka Sanjay
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Select Example</title>
<script type="text/javascript">
function fillSelBox(srcElem, destName) {
var selOne = srcElem;
var frm = srcElem.form;
var selTwo = frm.elements[destName];
var map =
{"fruit":
{"default": "- Select -", mango: "Mango", custardApple: "Custard Apple"},
"vegetable":
{"default": "- Select -", bitterGourd: "Bitter Gourd", onion: "Onion"},
"fastFood":
{"default": "- Select -", noodle: "Noodles", soup: "Soup"},
"default":
{"default": "- Select -"}
};
selBoxUtil(selOne, selTwo, map);
}
function selBoxUtil(src, dest, map) {
if(!src || !dest || !map) {
return;
}
removeAll(dest);
var selected = src.options[src.selectedIndex].value;
for(var key in map) {
if(key == selected) {
var valueMap = map[key];
for(var innerKey in valueMap) {
var val = valueMap[innerKey];
addOption(dest, val, innerKey);
}
}
}
}
function addOption(e, value, label) {
if(!e) {
return;
}
var o = new Option(value, label);
try {
e.add(o);
} catch(ee) {
e.add(o, null);
}
}
function removeAll(e) {
if(e && e.options) {
e.options.length = 0;
}
}
</script>
</head>
<body id="bdy">
<form id="frm" action="#">
<div id="container">
<select name="selOne" id="selOne" onchange="fillSelBox(this, 'selTwo');">
<option value="default">- Select -</option>
<option value="fruit">Fruits</option>
<option value="vegetable">Vegetables</option>
<option value="fastFood">Fast Food</option>
</select>
<select name="selTwo" id="selTwo">
<option value="default">- Select -</option>
</select>
</div>
</form>
</body>
</html>