PDF Editing Issues

How to Use If-Else JavaScript for PDF Form Fields

Anum MushtaqAnum Mushtaq Nov 3, 2026 869 views

Question details

The user wants to set a secondary PDF form field to remain blank when a drop-down list is empty or set to 'Yes', and to display 'N/A' when the drop-down is set to 'No'.

How to Use If-Else JavaScript for PDF Form Fields
Product
Adobe Acrobat PDF
Device & OS
not provided
Scenario
Creating an interactive PDF form where a text field's value dynamically updates based on the user's selection in a corresponding drop-down menu.
Observed behavior
The target text field evaluates the drop-down's export value using a custom script and automatically populates with 'N/A' or stays blank based on the conditional logic.
Before you start

Verify that your drop-down options have correctly configured 'export values' (like 'Yes' or 'No'), and ensure you are using JavaScript rather than Java for the PDF script.

Solution 1Recommended

Apply a Custom JavaScript Calculation to the Target Field

Use a simple conditional JavaScript snippet in the target text field's calculation properties to read the drop-down selection and assign the correct output.

In interactive PDF forms, a calculation script placed in the target field (Field 2) can continuously monitor another field (the drop-down). By comparing the drop-down's selected export value as a string, you can update the target field to display specific text.

The following script uses a ternary operator as a shorthand for an If-Else statement, keeping the calculation efficient.

1
Open form editing mode

Open your PDF document in Acrobat and select 'Prepare Form' from the tools menu to access form editing capabilities.

2
Access target field properties

Right-click the target text field (the one that should display 'N/A' or remain blank) and select 'Properties'.

3
Enable custom calculation

Navigate to the 'Calculate' tab, select 'Custom calculation script', and then click the 'Edit' button.

4
Enter the JavaScript

Type the following script: var value = this.getField('Dropdown1').valueAsString; event.value = (value == 'No') ? 'N/A' : ''; (Ensure you replace 'Dropdown1' with the exact name of your drop-down field).

5
Test the form logic

Click 'OK', close the properties dialog, and click 'Preview' to test selecting 'Yes', 'No', and leaving the drop-down blank to ensure the script works.

Apply a Custom JavaScript Calculation to the Target Field
Exact Match Required: The string evaluation is case-sensitive. Ensure that the text 'No' in your script perfectly matches the export value defined in the drop-down's Options tab.
Edit PDF Forms Easily

Create and Manage Interactive PDF Forms with WPS Office

WPS Office provides a comprehensive PDF editor that allows you to effortlessly design, edit, and manage interactive PDF forms, handling text fields, check boxes, and drop-down lists with ease.

  1. 1. Open PDF in WPS Office: Launch WPS Office and open your existing PDF document.
  2. 2. Navigate to Form Tools: Click on the 'Form' tab located in the top ribbon to access interactive form tools.
  3. 3. Insert Form Elements: Select text fields, drop-down menus, or check boxes from the toolbar and click on the page to insert them.
  4. 4. Customize and Save: Double-click the inserted form fields to adjust their properties, add options, and save the finalized PDF document.
Create interactive PDF forms with drop-downs and text fields seamlesslyHighly compatible with Adobe Acrobat PDF formats and form elementsLightweight, fast, and completely free to download for standard office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF JavaScript calculation not working?

The most common reason for a failing script is mismatched export values or field names. Ensure your JavaScript uses the exact case-sensitive field name and the correct export value (e.g., 'No') defined in the drop-down's Options tab.

What is the difference between Java and JavaScript in PDF forms?

Java is a separate, standalone programming language. JavaScript (specifically Acrobat JavaScript) is the scripting language supported by PDF applications to run interactive elements like custom field calculations and validations.

Can I hide a field based on a drop-down selection instead of changing its text?

Yes. Instead of changing the event.value, you can use JavaScript to modify the display property of the field (for example, using 'this.getField("FieldName").display = display.hidden;') based on the evaluated drop-down selection.

Do I need Acrobat Pro to add custom calculation scripts to PDF forms?

Yes, generally creating interactive PDF forms and editing custom JavaScript calculations requires advanced PDF authoring software like Adobe Acrobat Pro or dedicated, fully-featured PDF form editors.