PDF Editing Issues

How to Set One PDF Form Field Based on Another Using JavaScript

Bushra ParveenBushra Parveen Nov 10, 2026 869 views

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.

How to Set One PDF Form Field Based on Another Using 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).
Before you start

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.

Solution 1Recommended

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.

1
Open Field B Properties

Open your PDF in Form Editing mode. Right-click on the destination field (Field B) and select 'Properties'.

2
Navigate to the Calculate Tab

Click on the 'Calculate' tab at the top of the Properties dialog box and select the 'Custom calculation script' option.

3
Enter the JavaScript Code

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

4
Customize and Save

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 Calculation Script in the Destination Field
Using event.value: When writing a calculation script inside the destination field, using 'event.value' is the standard method to assign the field's output.
Edit PDF Forms Easily

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. 1. Download and Install: Download WPS Office for free and install it on your device.
  2. 2. Open Your PDF: Launch WPS Office and open your interactive PDF document containing the form fields.
  3. 3. Fill the Form: Click on the form fields to enter your data. Any embedded calculations and JavaScript dependencies will execute smoothly.
  4. 4. Save and Export: Once the form is filled correctly, click 'Save' or export the document to securely share your mapped data.
Easily fill, view, and manage interactive PDF forms.Highly compatible with Adobe Acrobat PDF formats and embedded scripts.Lightweight, fast, and features a user-friendly interface.Includes Word, Excel, PowerPoint, and PDF tools in one comprehensive app.
microsoft office alternative - wps office

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.