PDF Editing Issues

Call a Document-Level JavaScript Function from a PDF Form Field

Amos GikundaAmos Gikunda Nov 19, 2026 868 views

Question details

The user needs a document-level JavaScript function to write to a PDF form field identified by a parameter.

Call a Document-Level JavaScript Function from a PDF Form Field
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.
Before you start

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.

Solution 1Recommended

Pass the destination field name as a string

Use this approach when the function should look up the destination field by name.

1
Define the function

In Acrobat, open the document JavaScripts editor and define the function: function diffDate(subStart, k9Start, targetName) { this.getField(targetName).value = "Hello"; }

2
Call the function from the field

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");

3
Test the action

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 name as a string
Pass the name, not the value: The third argument must be "PA2", not this.getField("PA2").value. The example accepts the two source values but does not yet use them to calculate a result.
Free Microsoft Office alternative

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.

View and work with PDF documents alongside other filesOpen and edit Microsoft Office formats such as DOCX, XLSX, and PPTXUse a familiar interface for everyday document tasksMove existing Office files into a lightweight workspace
microsoft office alternative - wps office

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.