iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Knowledge Base > Code Library > Dynamically populated JavaScript boxes


Reply
 
Thread Tools Display Modes
  #1  
Old 01-08-2003, 02:26 PM
Rezner's Avatar
Rezner Rezner is offline
C# Lover

Preferred language:
* Expert *
 
Join Date: Jan 2002
Location: 00-80-C8-C3-2E-52
Posts: 1,899
Default Dynamically populated JavaScript boxes

The below code is simple example I came up with to show how to utilize dynamic comboboxes with JavaScript. Doing so can do a few things:

1) Distribute more of the "work" to the client's system
2) Greatly increase the efficiency of your site by reducing the number of pages that need to be served up
3) Present a more user-friendly interface to your users

In the below example you must specify a city that is closest to you. Instead of listing many cities in one combobox, you first select what region of the United States you live in. A second combobox is then populated based on what cities are in that area. As you user, you now only have to select a few cities instead of all of them in one list. This is an easy concept and thing to do, but most sites you see don't utilize it:
Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">


/* 
1. Create a multi-dimensional array that holds the three main REGIONs of the United States

      Array Index	Region
      ------------------------
      0	                East Coast
      1                 Midwest
      2                 West Coast  


2. Each REGION will hold CITY values in its own array
*/

var REGION = new Array(   new Array(), new Array(), new Array()   );

function AddCITY(intRegion, strCity) {

//Purpose: Insert a new CITY into the specified REGION (intRegion 0-2)
 
  //insert a new CITY into this REGION and then increase its size
  REGION[ intRegion ][  REGION[ intRegion ].length++  ] = strCity;

}

function LoadCities() {

//Purpose: load the below CITY values into their corresponding arrays of the main REGION array

 //add the east coast cities (REGION=0)
 AddCITY(0,"New York, NY");
 AddCITY(0,"Bangor, ME");
 AddCITY(0,"Richmond, VA");
 AddCITY(0,"Miami, FL");
 AddCITY(0,"Atlanta, GA"); 

 //add the midwestern cities (REGION=1)
 AddCITY(1,"Chicago, IL");
 AddCITY(1,"Minneapolis, MN");
 AddCITY(1,"Des Moines, IA");
 AddCITY(1,"St. Louis, MO");
 AddCITY(1,"Madison, WI");

 //add the west coast cities  (REGION=2)
 AddCITY(2,"San Francisco, CA");
 AddCITY(2,"Reno, NV");
 AddCITY(2,"Portland, OR");

}

function UpdateCombos(intREGION) {

//Purpose: Update the cboCITY combobox by dumping the appropriate REGION array into it


//Resize the number of entries shown in the cboCITY combo to equal the number of cities in
//this REGION (intREGION)
document.frm.cboCITY.length = REGION[intREGION].length;

//For each CITY in the array, add it to the combobox
for (x=0; x < REGION[intREGION].length; x++)
  document.frm.cboCITY[x] = new Option(REGION[intREGION][x],REGION[intREGION][x]);


}
</SCRIPT>

</HEAD>

<BODY onLoad=LoadCities();>

<FORM NAME="frm">

<!-- The OPTION values of this combo coordinate with the "CITY" array indexes -->

<SELECT NAME="cboRegion" onChange=UpdateCombos(this.value); STYLE="width:135px;">
 <OPTION VALUE=0>East</OPTION>
 <OPTION VALUE=1>Midwest</OPTION>
 <OPTION VALUE=2>West</OPTION>
</SELECT>

<SELECT NAME="cboCITY" STYLE="width:140px;"></SELECT>

</FORM>

</BODY>
</HTML>
__________________
"Man is still the best computer we can put aboard a spacecraft...and the only one that can be mass produced with unskilled labor." - Wernher von Braun
Reply With Quote
  #2  
Old 05-23-2003, 12:06 PM
diver diver is offline
Senior Contributor

Preferred language:
* Expert *
 
Join Date: Jun 2001
Location: Illinois
Posts: 865
Default

Rezner,
Nice sample, thanks for putting this up here.
I have a scenario question for you.

<u>Assumption:</u>
Let's say that everything is working as planned... the user chooses a city and state in the second listbox which has been dynamically loaded (created empty w/HTML). Suppose the user chooses the 4th selection down in the EAST region, being "Miami, FL".

<u>Using the selections</u>
Now... assume that you have another button somewhere on the same form as these listboxes that carts the user off to another page... passing the 2 listbox (EAST, Miami) variable choices through the url to perform some type of database function. Works great.

<u>Persistency Question</u>
The user "returns" back to the home page where the listboxes are located. But... the selection in the second listbox has not stayed. The user chose "Miami, FL", right? What now? With asp... we just persist session variables and inside of the OPTION tags we can specifiy "selected", yes? How do you fix that situation with this scenario?

Would'nt ask you if I did not think you were a guru!
diver
__________________
Sometimes I do not get notification of your post from the forum, which causes a delay. I am apologizing in advance!
Reply With Quote
  #3  
Old 08-14-2003, 12:05 PM
Rezner's Avatar
Rezner Rezner is offline
C# Lover

Preferred language:
* Expert *
 
Join Date: Jan 2002
Location: 00-80-C8-C3-2E-52
Posts: 1,899
Default

Sorry for the 3 month delay in posting this reply.

Yeah, the only real good way to deal with what you're talking about would be to pass the option you want initially listed on the server-side and then do a JavaScript/ASP (or PHP, CF, etc) hybrid to load up the values.
__________________
"Man is still the best computer we can put aboard a spacecraft...and the only one that can be mass produced with unskilled labor." - Wernher von Braun
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Advertisement: