C
Christina
Hi,
I've been looking at some code for dependent list boxes to adapt to a State
and City list. There will only be 2 states for the first list box, and 3
cities in the second list box. When the state is selected, the relative
cities appear in the second box.
I found a nice set of code online that was derived from Dreamweaver code and
have altered it to simplify for my minimal needs. I have learned a lot of
coding from looking at things and breaking it down and I understand most of
this. What I don't understand here is how all of the arrays are being
populated. In some cases it looks like a variable for an array exists, but
I don't see where it originated - specifically arrDL. Can someone look at
this code and help with some additional comments as to what is actually
occurring?
The Javascript:
<script type="text/JavaScript">
var stateList = new Array();
var cityList = new Array();
cityList[1] = "listState"; // Name of parent list box
cityList[2] = "listForm"; // Name of form containing parent list box
cityList[3] = "listCity"; // Name of child list box
cityList[4] = "listForm"; // Name of form containing child list box
cityList[5] = stateList; // No need to do anything here
stateList[0] = "OR"
stateList[1] = "Salem"
stateList[2] = "Salem"
stateList[3] = "OR"
stateList[4] = "Bend"
stateList[5] = "Bend"
stateList[6] = "OR"
stateList[7] = "Portland"
stateList[8] = "Portland"
stateList[9] = "WA"
stateList[10] = "Seattle"
stateList[11] = "Seattle"
stateList[12] = "WA"
stateList[13] = "Spokane"
stateList[14] = "Spokane"
stateList[15] = "WA"
stateList[16] = "Vancouver"
stateList[17] = "Vancouver"
// End of object/array definitions, beginning of generic functions
function setCityList(arrDL){
var olistState = document.forms[arrDL[2]].elements[arrDL[1]];
var olistCity = document.forms[arrDL[4]].elements[arrDL[3]];
var arrList = arrDL[5];
clearDynaList(olistCity);
if (olistState.selectedIndex == -1){
olistState.selectedIndex = 0;
}
populateDynaList(olistCity, olistState[olistState.selectedIndex].value,
arrList);
return true;
}
function clearDynaList(oList){
for (var i = oList.options.length; i >= 0; i--){
oList.options = null;
}
oList.selectedIndex = -1;
}
function populateDynaList(oList, nIndex, aArray){
for (var i = 0; i < aArray.length; i= i + 3){
if (aArray == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i
+ 2]);
}
//oList.size=oList.length //You need to comment out this line of the
function if you use this mod
}
oList.selectedIndex = 0;
}
</script>
html:
<form name="listForm">
<table width="500" border="0" cellspacing="0" cellpadding="5">
<tr>
<th width="50%">Parent List Menu</th>
<th width="50%">Child List Menu</th>
</tr>
<tr>
<td align="center" valign="top">
<select name="listState" size="2" onchange="setCityList(cityList)">
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</select>
</td>
<td align="left" valign="top">
<p>
<select name="listCity" size="3">
</select>
</p>
</td>
</tr>
</table>
</form>
Thanks,
Christina
I've been looking at some code for dependent list boxes to adapt to a State
and City list. There will only be 2 states for the first list box, and 3
cities in the second list box. When the state is selected, the relative
cities appear in the second box.
I found a nice set of code online that was derived from Dreamweaver code and
have altered it to simplify for my minimal needs. I have learned a lot of
coding from looking at things and breaking it down and I understand most of
this. What I don't understand here is how all of the arrays are being
populated. In some cases it looks like a variable for an array exists, but
I don't see where it originated - specifically arrDL. Can someone look at
this code and help with some additional comments as to what is actually
occurring?
The Javascript:
<script type="text/JavaScript">
var stateList = new Array();
var cityList = new Array();
cityList[1] = "listState"; // Name of parent list box
cityList[2] = "listForm"; // Name of form containing parent list box
cityList[3] = "listCity"; // Name of child list box
cityList[4] = "listForm"; // Name of form containing child list box
cityList[5] = stateList; // No need to do anything here
stateList[0] = "OR"
stateList[1] = "Salem"
stateList[2] = "Salem"
stateList[3] = "OR"
stateList[4] = "Bend"
stateList[5] = "Bend"
stateList[6] = "OR"
stateList[7] = "Portland"
stateList[8] = "Portland"
stateList[9] = "WA"
stateList[10] = "Seattle"
stateList[11] = "Seattle"
stateList[12] = "WA"
stateList[13] = "Spokane"
stateList[14] = "Spokane"
stateList[15] = "WA"
stateList[16] = "Vancouver"
stateList[17] = "Vancouver"
// End of object/array definitions, beginning of generic functions
function setCityList(arrDL){
var olistState = document.forms[arrDL[2]].elements[arrDL[1]];
var olistCity = document.forms[arrDL[4]].elements[arrDL[3]];
var arrList = arrDL[5];
clearDynaList(olistCity);
if (olistState.selectedIndex == -1){
olistState.selectedIndex = 0;
}
populateDynaList(olistCity, olistState[olistState.selectedIndex].value,
arrList);
return true;
}
function clearDynaList(oList){
for (var i = oList.options.length; i >= 0; i--){
oList.options = null;
}
oList.selectedIndex = -1;
}
function populateDynaList(oList, nIndex, aArray){
for (var i = 0; i < aArray.length; i= i + 3){
if (aArray == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i
+ 2]);
}
//oList.size=oList.length //You need to comment out this line of the
function if you use this mod
}
oList.selectedIndex = 0;
}
</script>
html:
<form name="listForm">
<table width="500" border="0" cellspacing="0" cellpadding="5">
<tr>
<th width="50%">Parent List Menu</th>
<th width="50%">Child List Menu</th>
</tr>
<tr>
<td align="center" valign="top">
<select name="listState" size="2" onchange="setCityList(cityList)">
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</select>
</td>
<td align="left" valign="top">
<p>
<select name="listCity" size="3">
</select>
</p>
</td>
</tr>
</table>
</form>
Thanks,
Christina