Call a Document-Level JavaScript Function from a PDF Form Field
Question details
The user needs a document-level JavaScript function to write to a PDF form field identified by a parameter.

- Product
- Adobe Acrobat PDF forms
- Device & OS
- not provided
- Scenario
- A form field calls a document-level function with source values and a destination field.
- Observed behavior
- The function works with a hard-coded destination field name but fails when the destination field's value is passed instead of its name.
Open the PDF in Adobe Acrobat and check the exact names of the source and destination fields in Prepare a Form. Make sure JavaScript is enabled under Preferences > JavaScript.
Pass the destination field name as a string
Use this approach when the function should look up the destination field by name.
In Acrobat, open the document JavaScripts editor and define the function: function diffDate(subStart, k9Start, targetName) { this.getField(targetName).value = "Hello"; }
In Prepare a Form, open the calling field's Properties and add a Run a JavaScript action. Use: diffDate(this.getField("SDTP2").value, this.getField("K9S2").value, "PA2");
Close form editing, trigger the field action, and check that the PA2 field displays Hello. Confirm that PA2 exactly matches the destination field name if it does not.

Pass the destination field object directly
Use this alternative when the calling script already has access to the destination field object.
Work with PDFs and Office documents in WPS Office
WPS Office is a free, lightweight alternative for everyday PDF and Office document work. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files into your workflow. Use Acrobat to edit and test the PDF form JavaScript described above.

Frequently Asked Questions
Why does passing PA2's value fail?
this.getField("PA2").value passes the field's current contents, not its name. Pass "PA2" if the function calls getField(), or pass this.getField("PA2") if the function expects a field object.
Where do I add the field's JavaScript call?
In Acrobat's Prepare a Form tool, open the calling field's Properties, choose an action trigger, and add a Run a JavaScript action containing the function call.
Should the field-name parameter be in quotes inside getField()?
No. Use this.getField(targetName) to read the parameter. Writing this.getField("targetName") looks for a field literally named targetName.
What should I check if the destination field still does not update?
Check the exact field name in Prepare a Form, confirm that the action trigger runs, and verify that JavaScript is enabled under Acrobat Preferences > JavaScript.




