PDA

View Full Version : Help writing dynamic script



Humper
12-19-2006, 04:42 PM
I want to write a script so that when a user selects a building number it pulls all the apartment numbers for that building

I want to put it in two <select> </select> format..

so like this



<td>Building</td>
<td><select size="1" name="building" onchange="select_building()">
<option value="whateverbuildingtheypick">whateverbuildingtheypick</option>
</select></td>

<td>Apartment #</td>
<td><select size="1" name="apartment">
<option value="pullfrombuildingselection">pullfrombuildingselection</option>
</select></td>


lets say I have the following building and apartment numbers...



var rooms = new Array(
[ "01", [ "0111A", "0111B", "0111C", "0111D",
"0112A", "0112B", "0112C", "0112D",
"0113A", "0113B", "0113C", "0113D",
"0114A", "0114B", "0114C", "0114D",
"0121A", "0121B", "0121C", "0121D",
"0122A", "0122B", "0122C", "0122D",
"0123A", "0123B", "0123C", "0123D",
"0124A", "0124B", "0124C", "0124D",
"0131A", "0131B", "0131C", "0131D",
"0132A", "0132B", "0132C", "0132D",
"0133A", "0133B", "0133C", "0133D",
"0134A", "0134B", "0134C", "0134D",
"0143A", "0143B", "0143C", "0143D",
"0144A", "0144B", "0144C", "0144D" ] ],
[ "02", [ "0211A", "0211B", "0211C", "0211D",
"0212A", "0212B", "0212C", "0212D",
"0213A", "0213B", "0213C", "0213D",
"0214A", "0214B", "0214C", "0214D",
"0221A", "0221B", "0221C", "0221D",
"0222A", "0222B", "0222C", "0222D",
"0223A", "0223B", "0223C", "0223D",
"0224A", "0224B", "0224C", "0224D",
"0231A", "0231B", "0231C", "0231D",
"0232A", "0232B", "0232C", "0232D",
"0233A", "0233B", "0233C", "0233D",
"0234A", "0234B", "0234C", "0234D",
"0243A", "0243B", "0243C", "0243D",
"0244A", "0244B", "0244C", "0244D" ] ])



can someone help me write this.... BTW this is going to be in a php file.. I dont know if that make a difference because I am very new to javascript.. I guess what I am says is it probably need to loop through the apt number in the actually script...?? any help would be great.

jscheuer1
12-19-2006, 06:43 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function set_selects(loaded){
if(!document.createElement)
return;

var rooms = new Array(
[ "01", [ "0111A", "0111B", "0111C", "0111D",
"0112A", "0112B", "0112C", "0112D",
"0113A", "0113B", "0113C", "0113D",
"0114A", "0114B", "0114C", "0114D",
"0121A", "0121B", "0121C", "0121D",
"0122A", "0122B", "0122C", "0122D",
"0123A", "0123B", "0123C", "0123D",
"0124A", "0124B", "0124C", "0124D",
"0131A", "0131B", "0131C", "0131D",
"0132A", "0132B", "0132C", "0132D",
"0133A", "0133B", "0133C", "0133D",
"0134A", "0134B", "0134C", "0134D",
"0143A", "0143B", "0143C", "0143D",
"0144A", "0144B", "0144C", "0144D" ] ],
[ "02", [ "0211A", "0211B", "0211C", "0211D",
"0212A", "0212B", "0212C", "0212D",
"0213A", "0213B", "0213C", "0213D",
"0214A", "0214B", "0214C", "0214D"] ])

var set_rooms=function(n){
var apt = document.getElementsByName('apartment')[0];
for (var i_tem = 0; i_tem < apt.options.length; i_tem++)
apt.removeChild(apt.options[i_tem]);
apt.options.length=0;
for (i_tem = 0; i_tem < rooms[n][1].length; i_tem++)
if(apt.options[i_tem])
apt.options[i_tem].text=apt.options[i_tem].value=rooms[n][1][i_tem];
else{
apt.options.length++;
apt.options[i_tem].text=apt.options[i_tem].value=rooms[n][1][i_tem];
}
}, bldg = document.getElementsByName('building')[0];
if (loaded&&typeof loaded!='object'){
set_rooms(loaded);
}
else {
for (var i_tem = 0; i_tem < rooms.length; i_tem++)
if(bldg.options[i_tem])
bldg.options[i_tem].text=bldg.options[i_tem].value=rooms[i_tem][0];
else{
bldg.options.length++;
bldg.options[i_tem].text=bldg.options[i_tem].value=rooms[i_tem][0];
}
set_rooms(0);
}
}
onload=set_selects;
</script>
</head>
<body>
<table>
<tr>
<td>Building</td>
<td><select size="1" name="building" onchange="set_selects(this.options.selectedIndex);" >
<option value="">select building</option>
</select></td>

<td>Apartment #</td>
<td><select size="1" name="apartment">
<option value="">selct apartment</option>
</select></td>
</tr>
</table>

</body>
</html>

Humper
12-19-2006, 06:49 PM
thanks... got it work :)