Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci
Last active March 16, 2023 09:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelGeraci/f14d5277b910d9714a509c98f28e1e4b to your computer and use it in GitHub Desktop.
Save JoelGeraci/f14d5277b910d9714a509c98f28e1e4b to your computer and use it in GitHub Desktop.
Acrobat JavaScript: Change list values based on another list
/*
LICENSE:
acrojs_dependentList.js by Joel Geraci is licensed under a Creative Commons Attribution 4.0 International License.
https://creativecommons.org/licenses/by/4.0/
You are free to:
Share — copy and redistribute the material in any medium or format
Adapt — remix, transform, and build upon the material for any purpose, even commercially.
Setup:
For list boxes: Add this script to the "selection change" script of the master list box.
For combo boxes: Add this script to the "custom format" script of the dropdown box.
IMPORTANT!
Edit the following line to identify the field name of the dependent list box then edit the properties of the dependentListValues JSON object. Property names should correspond to the export values of the list items.
*/
var dependentListBoxFieldName = "dependentListBox";
var dependentListValues =
{
"animal": [
["Dogs", "dogs"],
["Cats", "cats"],
["Pandas", "pandas"]
],
"mineral": [
["Gold", "Au"],
["Silver", "Ag"],
["Lead", "Pb"]
],
"veggie": [
["Carrots", "carrots"],
["Broccoli", "broccoli"],
["Spinach", "spinach"]
]
};
/*
You probably don't need to change anything from here down
*/
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.");
}
@GMCenterprises
Copy link

My goodness Joel, 4 years later I just used your code on a form and it works like a charm. Clearly you know what you're doing. Thanks for sharing it's much appreciated.

@bradblr
Copy link

bradblr commented Nov 26, 2020

Hi Joel and others

I am new to PDF forms and I tried this code. It worked perfectly.

When I select Master List box item, the First Dependent List box items changes accordingly.
However I am unable to see any changes in Second Dependent List box items.

I want : When Master List box item is selects, the First Dependent List box item should change. And when I select First Dependent List box item, the Second List box item should change.

Master List Box >> First Dependent List Box >> Second Dependent List Box.
Example: Mineral >> Gold >> Au

Can anybody please help me with the actual code for three list boxes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment