How do I auto fill a dropdown list with a selection made in another dropdown list?

I have multiple dropdown lists on a form. I am concerned about two of them. I want the user to select from the first dropdown (1-Configuration) which has 5 items. Based on a selection from that dropdown, I want a second dropdown (1-Respirator Type) to auto fill with certain choices (i.e., Air Purifying, Airline, SCBA, etc.). I also need help with where to put the script. Thanks so much in advance!!! I'm using Acrobat 10. Ryan


Ryan Kamerzell


2 Answers

This can be done with relative ease.

You essentially have a table where each row stands for a possible configuration choice. The first item in a row is the configuration type, and the second and following items are the available options.

For your form, this translates into a 2-dimensional array, where each element of the main level is an array of the options for a given configuration type. As there are not many example items in the question, we will just call the items "item 1", "item 2", "configuration 1", "configuration 2" etc.

Now, for the implementation:

We first set up a document-level script, where we define the array, and then we also define the code to load the first dropdown, plus the function to load the second dropdown:

var confarr = new Array() ;
confarr[0] = ["select configuration","select item"] ;
confarr[1] = ["configuration 1","item 1","item 2","item 3"] ;
confarr[2] = ["configuration 2","item 4","item 5","item 6"] ;
// etc.

var coarr = new Array() ;
for (var c1 = 0 ; c1 < confarr.length ; c1++) {
coarr[c1] = [confarr[c1][0], c1] ;
}
this.getField("1-Configuration").setItems(coarr) ;

function loadRespType(ndx)
{
var rtarr = new Array() ;
rtarr[0] = ["select Respirator Type for " + confarr[ndx][0], 0]
for (var rt1 = 1 ; rt1 < confarr[ndx].length ; rt1++) {
rtarr[rt1] = [confarr[ndx][rt1], rt1]
}
this.getField("1-Respirator Type").clearItems() ;
this.getField("1-Respirator Type").setItems(rtarr) ;
}


In the Keystroke event of the field "1-Configuration", we add the following line of code:

if (!event.willCommit && event.changeEx > 0) { loadRespType(event.changeEx) }


And that should do it.

Note: It is possible that the field 1-Configuration does not load properly. This is a sign of timing issues, and to overcome this, you would delete the line

this.getField("1-Configuration").setItems(coarr) ;

from the document-level script, and add it to the PageOpen script of the opening page. If you have a multi-page form, you may in that case protect this line from being executed whenever you get to that page (a procedure which has been described several times in the past; if you can't find it, ask in a comment on how to do it).


Hope this can help.

Max Wyss.


Max Wyss   

There is a great description to do exactly what I wanted at http://acrobatusers.com/tutorials/programming-list-and-combo-fields-in-acrobat-
and-livecycle-forms.


Ryan Kamerzell   


Please specify a reason: