Skip to main content
Participant
Sep 17, 2019 9:05 PM UTC
Answered

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

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Sep 18, 2019 11:36 AM UTC

For the use of the variable value use this:
var dependentListBoxFieldName = newSub;

3 replies

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
Sep 18, 2019 11:36 AM UTC

For the use of the variable value use this:
var dependentListBoxFieldName = newSub;

Translate
Laner61Author
Participant
Sep 18, 2019 9:02 PM UTC

Bernd, this did work, but I only after I changed the variable for newSub from:

var newSub = this.getField("P" + currentPage + ".Form." + "Subcategory"); to:

var newSub = ("P" + currentPage + ".Form." + "Subcategory");

 

So that's working now, thanks! However, I need a different way to initiate the page determination script. Currently, I'm using it in the first field on the template page. When it gets focus, it runs the javascript

var currentPage = this.pageNum;

var newSub = ("P" + currentPage + ".Form." + "Subcategory");

 

This places a make-or-break scenario on someone clicking on the first field before the Category field. Is there another way I can have the currentPage and newSub variables triggered instead of relying on someone clicking on the first field?

 

Translate
Inspiring
Sep 17, 2019 11:41 PM UTC

Are you sure you have the correct parts of the name?

 

One could add a short script to debug the building of the names:

 

console.show();
console.println("Fieldl name: " + event.target.name);
var aName = event.target.name.split(".");
console.println("Number of sections for field name: " + aName.length);
for(var i in aName){
     console.println(i +": " + aName[i]);
}

 

With the information provided one should be able to compute the first two levels needed to be afixed to the renamed field names.

Translate