How do I assign a function to a DropDownList onChange in ScriptUI?
I'm pretty new to javascript and Script UI and need a little help joining a couple things I've learned together.
I'm trying to make a drop down list where the default value (Item 1) is not attached to a function (so it can be used as a title for the dropdown), but selecting a different item from the dropdown executes a function. Basic example here. This doesn't work BTW…
{
function myScript(thisObj) {
function myScript_buildUI(thisObj) {
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Panel Name", [0, 0, 300, 300]);
res = "group{orientation:'column', alignment:['fill', 'fill'], alignChildren:['fill', 'fill'],\
myDropDownList: DropDownList{properties:{items:['Item 1', 'Item 2', 'Item 3', 'Item 4']}},\
}"
// Adds resource string to panel
myPanel.grp = myPanel.add(res);
// DropDownList default selection
myPanel.grp.myDropDownList.selection = 0; /// Dropdown index starts at 0
// Assign function to UI elements
if (myPanel.grp.myDropDownList.value == 1) function() {
alert ("ALERT 1")
}
else if (myPanel.grp.myDropDownList.value == 2) function() {
alert ("ALERT 2")
}
else if (myPanel.grp.myDropDownList.value == 3) function() {
alert ("ALERT 3")
}
// Setup panel sizing and make panel resizable
myPanel.layout.layout(true);
myPanel.grp.minimumSize = myPanel.grp.size;
myPanel.layout.resize();
myPanel.onResizing = myPanel.onResize = function () {this.layout.resize();}
return myPanel;
}
// Build script panel
var myScriptPal = myScript_buildUI(thisObj);
if ((myScriptPal != null) && (myScriptPal instanceof Window)) {
myScriptPal.center();
myScriptPal.show();
}
}
// Execute script
myScript(this);
}
I have tested the alert function with a simple button, so I know it works here…
myPanel.grp.group2.alertTest.onClick = function() {
alert ("ALERT 1")
}
I think I basically need to say "if the value is equal to 1 then execute this function" but I'm unsure how to add that to the following line… (also I assume I need to add onChange and not onClick)
if (myPanel.grp.myDropDownList.value == 1)
