Joshua Badger Joshua Badger - 5 months ago 52
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

dropdownlist
.



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
dropdownlists
. However, I want to create
onChange
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
onChange
events work if these were known
dropdownlists
, 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
getDropDownLists(['mom','dad','baby'],family)
, I would get three
dropdownlists
:
familySrcLevel_0
,
familySrcLevel_1
,
familySrcLevel_2
. How would I then create
onClick
events for each of these
dropdownlists
, knowing that I won't always know how many there are? Is such a thing possible? It has to be done in Extendscript.

Answer

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