Reference javascript variable in a field name script
I've built an Acroform in Acrobat DC Pro. There are two linked dropdown lists, the 2nd (Subcategory) dependent on the 1st (Category). A javascript defines both the first list's items and the corresponding list's items as well. Here's the script, in the Custom Format Script of the field Category:
var dependentListBoxFieldName = "Subcategory";
var dependentListValues =
{
"Computer": [
["-Select-", "-Select-"],
["Desktop", "Desktop"],
["Laptop", "Laptop"],
["Smart phone", "Smart phone"],
["Tablet", "Tablet"],
["Other", "Other"]
],
"Hardware": [
["-Select-", "-Select-"],
["Headset", "Headset"],
["Keyboard", "Keyboard"],
["Monitor", "Monitor"],
["Mouse", "Mouse"],
["Printer", "Printer"],
["Scanner", "Scanner"],
["Other", "Other"]
],
"Software": [
["-Select-", "-Select-"],
["AV app", "AV app"],
["Browser", "Browser"],
["Email app", "Email app"],
["Media app", "Media app"],
["Office app", "Office app"],
["Social app", "Social app"],
["Utility", "Utility"],
["Other", "Other"]
],
};
if ((event.target.type == "combobox" && event.name == "Format") || (event.target.type == "listbox" && event.name == "Keystroke")) {
if (event.target.type == "combobox") {
if (dependentListValues.hasOwnProperty(event.target.value)) {
this.getField(dependentListBoxFieldName).setItems(dependentListValues[event.target.value]);
}
else {
this.getField(dependentListBoxFieldName).clearItems();
}
}
if (event.target.type == "listbox" && dependentListValues.hasOwnProperty(event.changeEx)) {
this.getField(dependentListBoxFieldName).setItems(dependentListValues[event.changeEx]);
}
}
else {
app.alert("This script was not intended for this field type or event.");
}
The issue I have is when the form is re-created for additional data entry. A button spawns a Form template and the hard-coded list name breaks because the field is renamed (a.spawn(numPages, true, false);). I have been able to determine the page number to get the new field name when the user clicks in the first text field of the form. This setup as a javascript run when the 1st text field gets focus:
var currentPage = this.pageNum;
var newSub = this.getField("P" + currentPage + ".Form." + "Subcategory");
And I wind up with something like P1.Form.Subcategory. But how do I code the result into the P1.Form.Category script on this page (and subsequent pages)?
Does the line var dependentListBoxFieldName = "Subcategory"; become var dependentListBoxFieldName = "newSub"; because that doesn't seem to work. I get an error in the console:
TypeError: this.getField(...) is null
229:AcroForm:P1.Form.Category:Format
That points to this line: this.getField(dependentListBoxFieldName), which is newSub.
I'm assuming my syntax is wrong - how do I reference the output of the newSub variable into the Category script? Should it be referenced as a string somehow?
Thanks,
Laner61
