How to Auto-Fill PDF Form Fields Using JavaScript Codes
Question details
The user needs a way to automatically populate multiple dependent fields in an interactive PDF form when a specific secret code is entered into a trigger field.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Designing an interactive PDF form where entering a specific input triggers automatic data population in designated fields.
- Observed behavior
- When the correct code is typed, dependent fields should populate with predefined values. If the code is incorrect or removed, the fields should remain blank.
Ensure you have a PDF editor that supports form field creation and JavaScript execution, and write down the exact names of the target fields you want to populate.
Add a Custom JavaScript Script to the Code Field's Validation Event
Use a custom JavaScript snippet in the target field's properties to evaluate the entered code and populate other fields accordingly.
Interactive PDF forms can execute custom JavaScript to create dynamic behaviors. By attaching a script to the primary input field's validation or exit event, the PDF will automatically evaluate what the user types and update other fields in real time.
Open your PDF document in your PDF editor and enter the 'Prepare Form' or 'Edit Form' mode so that all interactive fields are highlighted.
Right-click the text field where users will type the secret code and select 'Properties' from the context menu.
Navigate to the 'Validate' or 'Format' tab. Choose the option to 'Run custom validation script' or similar, and click 'Edit' to open the script editor.
Paste the following code: if (event.value == "HAT") { this.getField("Points").value = "100"; this.getField("Cash").value = "$10"; } else { this.getField("Points").value = ""; this.getField("Cash").value = ""; } Replace 'HAT' with your secret code, and 'Points'/'Cash' with your actual field names.
Click 'OK' to save the script, close the properties window, exit form editing mode, and test the form by typing the code into the field.
Easily Create and Edit Interactive PDF Forms with WPS Office
WPS PDF provides a comprehensive set of tools to create, edit, fill, and manage interactive PDF forms quickly and accurately across all your devices.
- 1. Install the software: Download and install WPS Office Free on your computer.
- 2. Open the PDF tool: Launch the application and select 'PDF' from the main dashboard.
- 3. Load your document: Open your PDF file and navigate to the 'Form' tab to interact with, edit, or fill fields seamlessly.

Frequently Asked Questions
How do I handle multiple secret codes in my PDF form?
You can handle multiple secret codes by expanding the JavaScript with 'else if' statements. For example: if (event.value == "CODE1") { ... } else if (event.value == "CODE2") { ... } else { ... } to define different outputs based on different inputs.
Why aren't my dependent fields updating after I enter the code?
This usually happens if the script contains a syntax error, or if the field names in the script don't exactly match the field names in the PDF. Double-check your spelling, capitalization, and ensure JavaScript is enabled in your PDF viewer's preferences.
Do these auto-fill scripts work on mobile PDF readers?
Not always. Many built-in or lightweight mobile PDF readers strip out or ignore JavaScript for security and performance reasons. To ensure the script works, users should open the form using robust desktop software like WPS PDF or Adobe Acrobat.




