How to Populate PDF Form Fields with Random Options Using JavaScript
Question details
The user needs to automatically populate specific fields in a fillable PDF with randomly selected predefined values (such as B, C, or D) when a value is entered into another source field.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating advanced dynamic fillable PDF forms with automated data entry behavior.
- Observed behavior
- Destination fields are successfully populated with a random value from an array triggered by user interaction with a specified source field.
Ensure you are editing the form in a dedicated desktop PDF application that supports advanced document JavaScript, as basic browser-based PDF viewers often fail to execute Acrobat-style scripts correctly.
Create a Custom JavaScript for PDF Form Fields
Use a validation, calculation, or keystroke script on the source field to randomly select a value from a predefined array and push it to the destination fields.
To achieve dynamic form filling, you need to embed custom JavaScript into the properties of your PDF form fields. The script will define an array of allowed options, pick a random item from that array, and set the value of the target fields automatically.
Depending on when you want the random value to be generated, you can attach this script to the source field's calculate, validate, or format events.
Launch your advanced PDF editor, open the fillable PDF document, and access the form editing or prepare form mode to reveal all interactive fields.
Right-click the source field (the one the user interacts with) and select 'Properties'. Go to the 'Calculate', 'Validate', or 'Format' tab based on your trigger preference.
Choose the option to run a custom JavaScript. Input a script that defines your array of options (e.g., var options = ['B', 'C', 'D'];), selects a random item using Math.random(), and assigns it using this.getField('DestinationField').value = selectedItem;
Save your changes, exit form editing mode, and test the source field. Ensure you test this in a desktop application to verify the JavaScript executes without browser limitations.

Edit and Manage PDF Forms Seamlessly with WPS Office
WPS Office offers a comprehensive built-in PDF editor that makes creating, filling, and managing interactive PDF forms incredibly easy. With an intuitive interface, handling complex document tasks becomes highly efficient.
- 1. Download and install: Get WPS Office from the official website and easily install it on your computer.
- 2. Open the PDF file: Launch WPS PDF and open the interactive form document you wish to edit or fill.
- 3. Access Form tools: Navigate to the 'Form' tab on the top ribbon to view all available interactive field options and properties.
- 4. Edit and save: Modify the text fields, input necessary data, and save your document securely to preserve all functionality.

Frequently Asked Questions
Why is my PDF JavaScript not working when opened in Chrome?
Most modern web browsers, including Chrome and Edge, use lightweight, built-in PDF viewers that do not fully support complex Acrobat-style JavaScript. To ensure scripts run properly, the PDF must be opened in a dedicated desktop PDF editor or viewer.
How do I reference another field in a PDF JavaScript?
You can reference other fields in the document by using the method this.getField('ExactFieldName') in your script. This allows you to retrieve its current value or dynamically assign a new value to it.
Can I generate random numbers instead of text options in a PDF form?
Yes, absolutely. Instead of pulling from an array of text strings, you can use standard JavaScript math functions like Math.floor(Math.random() * 10) + 1 to generate random numbers and push those values to your destination fields.
Is there a way to trigger the script as the user types?
Yes, you can assign your custom JavaScript to the 'Keystroke' event in the field's properties. This ensures the script evaluates and runs in real-time with each key pressed in the source field.




