How to Use If-Else JavaScript for PDF Form Fields
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'.

- 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.
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.
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.
Open your PDF document in Acrobat and select 'Prepare Form' from the tools menu to access form editing capabilities.
Right-click the target text field (the one that should display 'N/A' or remain blank) and select 'Properties'.
Navigate to the 'Calculate' tab, select 'Custom calculation script', and then click the 'Edit' button.
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).
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.

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. Open PDF in WPS Office: Launch WPS Office and open your existing PDF document.
- 2. Navigate to Form Tools: Click on the 'Form' tab located in the top ribbon to access interactive form tools.
- 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. Customize and Save: Double-click the inserted form fields to adjust their properties, add options, and save the finalized PDF document.

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.




