473,785 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dependant list boxes

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(arr DL){

var olistState = document.forms[arrDL[2]].elements[arrDL[1]];
var olistCity = document.forms[arrDL[4]].elements[arrDL[3]];
var arrList = arrDL[5];

clearDynaList(o listCity);

if (olistState.sel ectedIndex == -1){
olistState.sele ctedIndex = 0;
}

populateDynaLis t(olistCity, olistState[olistState.sele ctedIndex].value,
arrList);
return true;
}

function clearDynaList(o List){

for (var i = oList.options.l ength; i >= 0; i--){
oList.options[i] = null;
}

oList.selectedI ndex = -1;
}

function populateDynaLis t(oList, nIndex, aArray){
for (var i = 0; i < aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.l ength] = new Option(aArray[i + 1], aArray[i
+ 2]);
}
//oList.size=oLis t.length //You need to comment out this line of the
function if you use this mod
}

oList.selectedI ndex = 0;
}

</script>

html:
<form name="listForm" >
<table width="500" border="0" cellspacing="0" cellpadding="5" >
<tr>
<th width="50%">Par ent List Menu</th>
<th width="50%">Chi ld List Menu</th>
</tr>
<tr>
<td align="center" valign="top">
<select name="listState " size="2" onchange="setCi tyList(cityList )">
<option value="OR">Oreg on</option>
<option value="WA">Wash ington</option>
</select>
</td>
<td align="left" valign="top">
<p>
<select name="listCity" size="3">
</select>
</p>

</td>
</tr>
</table>
</form>

Thanks,
Christina
Nov 9 '06 #1
1 4388
"Christina" <de******@centu rytel.netwrote in message
news:58******** *************** *******@century tel.net...
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.
I've gone through the code and tested and made changes to some of the
variable names and discovered that often one 'new' variable name actually
refers to the previous variable name, so I changed them to reflect this. I
combined the functions into one function and commented as much as I could to
track what was happening and it works fine.

Now I need to take a checked radio button that selects a state and make it
select the corresponding state in the list box, thereby also filling the
city list box. This is for a class, and although it seems redundant, it's
what the instructor wants. He hasn't provided any guidelines or instructions
on how to do this, so I am asking the experts here. Also, note that he wants
the City list to be disabled on load, then enabled when a state selection is
made through the list or radio buttons. I don't know why - the city list is
supposed to be empty until a state is selected - so who's going to know if
it's disabled or not? I have pulled the value of the radio button out with
a loop. Now I need to match that value to the list box value so it can be
automatically selected.

This is the revised js code:

<script type="text/JavaScript">
var listValues = new Array(); //array to hold all values
var selectElem = new Array(); //array to hold each value

selectElem[1] = "State"; // Name of parent list box
selectElem[2] = "listForm"; // Name of form containing parent list box
selectElem[3] = "City"; // Name of child list box
selectElem[4] = "listForm"; // Name of form containing child list box
selectElem[5] = listValues; // 5th element is full array of values

listValues[0] = "OR" //value of state in state list
listValues[1] = "Salem" //text of city in city list
listValues[2] = "SAL" //value of city in city list

listValues[3] = "OR"
listValues[4] = "Bend"
listValues[5] = "BEN"

listValues[6] = "OR"
listValues[7] = "Portland"
listValues[8] = "PDX"

listValues[9] = "WA"
listValues[10] = "Seattle"
listValues[11] = "SEA"

listValues[12] = "WA"
listValues[13] = "Spokane"
listValues[14] = "SPK"

listValues[15] = "WA"
listValues[16] = "Vancouver"
listValues[17] = "VAN"

function activate()
{
document.listFo rm.City.disable d=false;
for (i=0;i<document .listForm.radSt ate.length;i++)
{
if (document.listF orm.radState[i].checked)
{
radSelect = document.listFo rm.radState[i].value;
}
}
}

function setCity(selectE lem){ //put the values in the city list

var listState = document.forms[selectElem[2]].elements[selectElem[1]];
//assign state list array elements to variable
var listCity = document.forms[selectElem[4]].elements[selectElem[3]];
//assign city list array elements to variable
var fullList = selectElem[5]; //assign variable to full list array

//clear list
for (var i = listCity.option s.length; i >= 0; i--){
listCity.option s[i] = null;
}

listCity.select edIndex = -1;

//populate list
var selectedState = listState[listState.selec tedIndex].value;

for (var i = 0; i < fullList.length ; i= i + 3){
if (fullList[i] == selectedState){
listCity.option s[listCity.option s.length] = new Option(fullList[i + 1],
fullList[i + 2]);
}
document.listFo rm.City.disable d=false;

}

return true;
}
</script>

and the revised form:

<form name="listForm" >
<table border="0" cellspacing="0" cellpadding="5" >
<tr>
<td><input type="radio" name="radState" value="OR"
onClick="activa te()" />
<label>Oregon </label>
</td>
<td><input type="radio" name="radState" value="WA"
onClick="activa te()" />
<labelWashingto n</label>
</input>
</td>
</tr>
<tr>
<td align="center" valign="top">
<select name="State" size="2" onchange="setCi ty(selectElem)" >
<option value="OR">Oreg on</option>
<option value="WA">Wash ington</option>
</select>
</td>
<td align="left" valign="top">
<p>
<select name="City" size="3" disabled="true" >
</select>
</p>
</td>
</tr>
</table>
</form>

Thanks to anybody who give me a direction to go with this.

Christina
Nov 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2461
by: PT | last post by:
I got a form with many text boxes, checkboxes and 3 drop downs. From that 3, 2 are dependant. I can choose one drop down, and the next drop down should display the dependant values of the first drop down chosed. And I have a submite button to submit all these values to DB. First problem: I cant keep the value of the selected text in the first drop down. It always goes back to the first value. (but the asp site extension changes...
2
2563
by: nadia | last post by:
I am have used php to create a two sets of arrays of listboxes, each of the listbox in the array have a unique ID. One of the list boxes are dependant on the other one. I have written the code in javascript, well changed existing code. I need to be able to select the element by ID rather than name. The code was working before I changed things to "getElementByID". I am not very good at javascript so I am not sure if I am on the right track,...
8
2585
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and courses are populated. When course is selected, lists of occurrences, groups and
2
12623
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will select any skill in 1st drop down list then i'll select % of this skill in the 2nd list box , based on the percentage i've selected in the 2nd list box it has to display 2 sets of drop down list boxes at run time one for selecting skill and
12
2423
by: StevoGman | last post by:
To Whomever Can Help Me, I am a very inexperienced programmer, and I am trying to create a combo box that is dependant on another combo boxes answer. For example: I have a combo box (Study Abroad), a Catvar, with three picks Yes;No;Unknown. If yes is selected then the following combo box (Study Abroad Programs) becomes availible to choose picks from. If other two picks are selected Study Abroad Programs should become greyed out. ...
7
4526
by: ljungers | last post by:
Have Form-1 with 3 text boxes and 1 command button. With any of the 3 boxes filled out and button is clicked, a Macro is performed that Opens a Query that has a WHERE clause that uses the 3 test boxes of data from form-1. 1) How do I cause the SQL command window not to show for that SQL command? The query results is placed in a ListBox on Form-2 and desired rows in the list box are selected/clicked, followed by a click on a button that...
12
9877
by: ljungers | last post by:
I'm on the home streach of my project and found that my "Reset for New Search" command button not working as desired. What should happen is that when the button is clicked a Event Procedure is run. That event procedure should clear the text boxes that are used for a search query that loads the results into a list box on that same form. When clicked, all is cleared in the text boxes, and the list box is cleared but shows column seperator lines...
2
1392
by: spudpeel | last post by:
Hi, I am currently making a document control system through access, and yet another hurdle has come up. Rather than give the user a table to sift through, I want to be able to use dependant combo boxes to pick a document name from a list. There are two tables involved: the first contains Department codes and Department names, i.e. 1.Sales, 2.Production; and the second contains Document Details, including the Department Code. I want the first...
18
1561
by: fjm | last post by:
I got a winner here.. I need a solution that will use a single html select and two input fields that depend on the value selected. This dropdown will have 3 values. If a certain values is selected, I need two input boxes to appear. If either of the other two values are selected, then no additional input boxes should be added. This sounds like a job for AJAX i'm sure but I haven't a clue about AJAX. What I have now is a select tag...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.