java & javascript problem

wxdqz

New Member
hello all,
i am having some difficulty trying to create a javascript dropdown menu (menu1) which will modify the other dropdown menu (menu2) on the page based on the selection made in menu1.

i am generating the javascript code from within a java servlet which makes database calls to determine the values for menu1. once menu1 has been generated, the values will not change for the duration of the session. the data which will populate menu2 is obtained by database calls with the selection from menu1 as the key. since it seemed like that is not possible from javascript i decided to preload all such data during the init() of my servlet.

here's some of the code:

public void doPost(...)
{
...
/* js funcs */
out.println("var masterArray = new Array(" + groupNames.length + ");");

// map HashMap contents to javascript multidimensional arrays
Set keys = groupJobs.keySet();
int k=0;
for (Iterator it=keys.iterator(); it.hasNext(); k++) {
String[] jobs = (String[]) groupJobs.get(it.next());
out.print("masterArray[" + k + "] = new Array(");
for (int i=0; i<jobs.length; i++) {
if (i == jobs.length - 1)
out.print("\"" + jobs + "\")");
else
out.print("\"" + jobs + "\", ");
}
if (it.hasNext())
out.println("; ");
}
out.println(";");

// js function
out.println("function setJobs(selectedGroup) {");
out.println("\tvar jobList = masterArray[selectedGroup];");
out.println("\twith (window.document.jobsForm.jobSelect) {");
out.println("\t\tfor (var i=options.length-1; i>0; i--) { options = null; }");
out.println("\t\tfor (var i=1; i<masterArray[selectedGroup].length; i++) { options[i-1] = masterArray[selectedGroup]; }");
out.println("\t}");
out.println("}");
// end javascript
...
out.println("<form name=\"groupForm\">");
out.println("<select name=\"groupSelect\" onChange=\"setJobs(this.options.selectedIndex)\">");

for (int i=0; i<groupNames.length; i++)
{
out.println("<option value=http://www.webdeveloper.com/forum/archive/index.php/\"" + i + "\">" + groupNames);
}
out.println("</select></form>");
...
}

the java variable groupJobs is a HashMap of groupnames mapped to their corresponding jobnames. i can load the jobname arrays into the masterArray, however, my javascript function setJobs() does not work properly, particularly the following for loop: for (var i=1; i<masterArray[selectedGroup].length; i++) { options[i-1] = masterArray[selectedGroup];}

totally stumped with javascript. thanks in advance for your help!
 
Back
Top