How to Set One PDF Form Field Based on Another Using JavaScript
Question details
The user wants to automatically populate a destination PDF form field based on the input of a source field using custom mapping in Acrobat JavaScript.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms where dependent fields require specific output values (e.g., percentages or discrete numbers) rather than sequential or mathematical formulas.
- Observed behavior
- The user needs field B to display a specific, custom-mapped value (like 12 or 24) when field A is set to a specific number (like 19).
Ensure your PDF editor supports JavaScript for form fields, and note the exact names of your target form fields (e.g., 'Field A' and 'Field B') before applying the script.
Use a Calculation Script in the Destination Field
Applying a calculation script directly to the destination field (Field B) is the recommended approach, as it automatically updates whenever the source field changes.
By setting a custom calculation script in Field B, the PDF viewer will evaluate the script every time any field data changes, ensuring your mapped values are always synchronized with Field A.
Open your PDF in Form Editing mode. Right-click on the destination field (Field B) and select 'Properties'.
Click on the 'Calculate' tab at the top of the Properties dialog box and select the 'Custom calculation script' option.
Click the 'Edit' button and enter the following script: var value = this.getField("A").valueAsString; var map = {"17":"3%","18":"4%","19":"12","20":"24"}; event.value = map[value] || "";
Replace 'A' with the exact name of your source field, and update the values in the 'map' object to fit your specific requirements. Click 'OK', close the properties, and test the form.

Use a Validation Script in the Source Field
Alternatively, you can attach a script to the source field (Field A) that pushes the corresponding value to Field B when data entry is completed.
Use WPS Office to Fill and Manage PDF Forms
WPS Office provides robust PDF editing capabilities, including seamless interaction with interactive PDF forms, JavaScript calculations, and advanced document management.
- 1. Download and Install: Download WPS Office for free and install it on your device.
- 2. Open Your PDF: Launch WPS Office and open your interactive PDF document containing the form fields.
- 3. Fill the Form: Click on the form fields to enter your data. Any embedded calculations and JavaScript dependencies will execute smoothly.
- 4. Save and Export: Once the form is filled correctly, click 'Save' or export the document to securely share your mapped data.

Frequently Asked Questions
Why isn't my PDF form field calculating automatically?
Ensure that the 'Calculate form fields' preference is enabled in your PDF viewer settings. Additionally, check your JavaScript code for typographical or syntax errors, such as missing semicolons or incorrect field names.
Can I use text strings instead of numbers for the source field?
Yes, the custom dictionary map in the JavaScript code can use string values as keys. For instance, you can use {"High":"100", "Low":"10"}. Just ensure the keys match the exact text entered into the source field.
What is the difference between calculation and validation scripts in PDFs?
A calculation script on a destination field runs automatically whenever any field in the entire PDF document changes, keeping values constantly updated. A validation script on a source field only triggers when the user specifically finishes editing that particular field.




