Joshua Badger Joshua Badger - 1 year ago 183
Javascript Question

Extendscript: Dynamically create dropdown lists and onChange functions for them

Here's a challenging one. I'm relatively new to scripting but have an idea I want to get working.

I have a script that is dynamically generating drop-down lists based on an array: each item in that array creates a


function getDropDownLists(inputArray, grp) { //input an array and the UI Group we're adding these DDLs to
try {
eval(grp + "Array = [];"); //Creates an array to store the DDLs we're about to create
var listName; //Create variable to store name of DDL we're creating as we iterate through inputArray
for (var i = 0; i < inputArray.length; i++) {
listName = grp + "SrcLevel_" + i.toString(); //Get the name for the DDL we're about to create
eval('var ' + listName + ' = ' + grp + '.add("dropdownlist",[0,0,150,25])'); //add a DDL for the current array item
eval(listName + '.add("item","' + listName + '")'); //This line creates an item in each dropdown to tell me its name
eval(grp + "Array[" + i + "] = " + listName + ";"); //Adds newly created DDL to the storage array
} catch (e) {
alert("Error on line " + e.line + ":\n" + e.message);

When I call this function (it may not work perfectly here as I've cleaned it up a bit for display purposes) it correctly creates all my
. However, I want to create
events for each of these to reference the previous one in the created storage array and change its contents. I know how to make the
events work if these were known
, but every project I'll be working on is different and I'd like to get this to work without having to retool every time the project requirements change.

For example, when I call
, I would get three
. How would I then create
events for each of these
, knowing that I won't always know how many there are? Is such a thing possible? It has to be done in Extendscript.

Answer Source

The solution was to create another eval statement that contained the onChange function in it at the end of the function above.