How to Add Values to the Next Empty PDF Form Field with JavaScript
Question details
The user wants to use a JavaScript button to insert selected product data into the next available empty set of PDF form fields without overwriting existing entries.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form where clicking a button adds an item to a list of fields sequentially, similar to a shopping cart.
- Observed behavior
- The user needs a script that loops through existing fields, checks if they are empty, populates the first empty one it finds, and then stops execution.
Ensure your PDF form fields are named sequentially (e.g., Product1, Product2, Price1, Price2) so they can be easily referenced and iterated through in a JavaScript loop.
Use a JavaScript For Loop to Find and Populate Empty Fields
Implement a loop that iterates through your sequentially named form fields, checks for empty values, and inserts the data into the first available slot.
By naming your fields with a consistent prefix and a sequential number, you can use a simple `for` loop to check each field's value dynamically. The `valueAsString.length` property is an effective way to determine if a field is currently empty.
Once the script finds an empty field, it assigns the new values and uses the `break` statement to stop the loop. This is crucial, as it prevents the same data from filling all subsequent empty fields in the document.
Open your PDF in a form editing tool and ensure your target fields are named sequentially (e.g., "Product1" to "Product8" and "Price1" to "Price8").
Right-click your trigger button (e.g., the "Add to Cart" button) and open its Properties dialog.
Navigate to the Actions tab, select "Run a JavaScript" from the Select Action dropdown menu, and click the Add button.
In the JavaScript Editor, enter the following code: `for (var i = 1; i <= 8; i++) { if (this.getField("Product" + i).valueAsString.length == 0) { this.getField("Product" + i).value = this.getField("Dropdown20").valueAsString; this.getField("Price" + i).value = this.getField("Text14").valueAsString; break; } }`
Click OK to save the script, close the properties dialog, and click your button to test if the values populate the correct empty fields.

Create and Edit Interactive PDF Forms in WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to create interactive forms, add text fields, buttons, and manage document workflows efficiently without needing expensive software.
- 1. Open your PDF: Launch WPS Office and open the PDF document you want to turn into an interactive form.
- 2. Access form tools: Navigate to the "Form" tab on the top ribbon to reveal the form creation tools.
- 3. Add form controls: Use the form controls to add text fields, buttons, or dropdowns to your document canvas.
- 4. Adjust properties: Double-click any form field to adjust its properties, name, appearance, and export values.

Frequently Asked Questions
Why does my JavaScript overwrite all empty fields instead of just one?
This happens if you forget to include the `break;` statement inside your `if` condition. The `break` command tells the loop to stop executing immediately once the first empty field is found and populated.
How do I check if a PDF form field is empty using JavaScript?
The most reliable method is to check the length of the field's value as a string. You can use `this.getField("FieldName").valueAsString.length == 0` in your conditional statement to verify if it is blank.
Can I use this script for dropdown menus and checkboxes?
Yes, you can adapt the script to pull values from dropdowns (as shown with "Dropdown20" in the example) or set the export values of checkboxes, provided you reference the correct field names and value types in your script.
Do I need to name my fields in a specific way for the loop to work?
Yes, the loop relies on sequential naming (e.g., Item1, Item2, Item3). This allows the JavaScript `for` loop to concatenate the base name with the loop counter variable (`i`) to target each field dynamically.




