Universal Related Popup Menus / Another Way
Another Way
Universal Related Popup Menus
Another way to approach the code is to first fill a two-dimensional array and create the new options on the fly. This technique makes it easier to add new menu items, and also extend this technique to another dimension. Here's a stripped down version for download.
|
Related Select Lists
Choose a subject: |
---> | Choose a topic: |
The menus work identically to the previous page, and degrade gracefully. The array creation code has gone 2-D and now pregenerates the menu array outside the relate function.
- var catsIndex = -1;
var itemsIndex;
function newCat(){
catsIndex++;
a[catsIndex] = new Array();
itemsIndex = 0;
}
function O(txt,url) {
a[catsIndex][itemsIndex]=new myOptions(txt,url);
itemsIndex++;
}
function myOptions(text,value){
this.text = text;
this.value = value;
}
// fill array
newCat();
O("3-D Animation","/3d/");
O("Design","/dlab/");
O("DHTML","/dhtml/");
O("E-Commerce","/ecommerce/mm/");
O("Graphics","/graphics/");
O("HTML","/html/");
O("JavaScript","/js/");
newCat();
O("Authoring","/authoring/");
...
Can You Relate?
The relate function, which changes the second menu based on the first menu's selection, has been stripped of the on the fly array creation and now uses the 2-D array we created above to change the second menu's options on the fly.
- function relate(formName,elementNum,j) {
if(v){
var formNum = getFormNum(formName);
if (formNum>=0) {
formNum++; // reference next form, assume it follows in HTML
with (document.forms[formNum].elements[elementNum]) {
for(i=options.length-1;i>0;i--) options[i] = null; // null out in reverse order (bug workarnd)
for(i=0;i<a[j].length;i++){
options[i] = new Option(a[j][i].text,a[j][i].value);
}
options[0].selected = true;
}
}
} else {
jmp(formName,elementNum);
}
}
The guts of the relate function remain the same with two changes. The second menu's options array are now filled on the fly with the new Option command referring to the text and value properties of the a[j][i] element of our 2-D array. The aln2=a.length kludge was also replaced by calculating the length of the selectedIndex element of the 2-D array a (the number of options to be overwritten on the second menu).
This technique of pregenerating our options array can be extended into another dimension to create three related menus.
The full redirect.cgi script is also available in text form.
Comments are welcome
Created: Mar. 9, 1997
Revised: July 22, 1999
URL: https://webreference.com/dev/menus/intro2.html