How to Create Form Rows with Incrementing Field Names in PDF JavaScript
Question details
The user needs to dynamically add repeated rows of fields with sequential names in a PDF form using JavaScript, but the counter variable resets to zero on each click.

- Product
- PDF Editors / Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Using a button with a JavaScript action to generate new rows of input fields dynamically in a PDF form.
- Observed behavior
- The variable used to increment the field names resets to zero every time the button script executes, preventing the generation of unique, sequential field names.
Before modifying your PDF form's JavaScript, ensure that JavaScript execution is enabled in your PDF editor's preferences and always keep a backup copy of your original form to prevent accidental layout corruption.
Store the Row Counter in a Hidden Form Field
Prevent the row counter from resetting by saving its value in a hidden text field rather than relying on a local script variable.
When you declare a variable like 'var i = 0' inside a button's mouse-up script, it initializes to 0 every single time the button is clicked. To preserve the count across multiple clicks, the value must be stored outside the script's local scope.
Enter form editing mode and add a new text field named 'RowCount' somewhere on your document. In its Properties, set its visibility to 'Hidden' so it does not show up on the final form.
In the hidden field's properties, set its default value to '0'. This acts as the starting point for your incrementing script.
Update your 'Add Row' button's JavaScript to retrieve the value from the hidden field using `var i = Number(this.getField('RowCount').value);`.
Use `i` to generate your new row's sequential field names (e.g., `FieldName + i`). Once the row is created, increment the counter (`i++`) and save it back to the hidden field using `this.getField('RowCount').value = i;`.

Create Rows in Advance and Toggle Visibility
Avoid complex dynamic field creation by designing all potential rows beforehand and using a simple script to show or hide them.
Use a Loop to Find the Highest Field Index
Calculate the next available sequence number dynamically by looping through all existing fields before creating a new row.
Create and Manage Interactive PDF Forms with WPS Office
Instead of struggling with complex JavaScript codes to duplicate and position form fields, you can easily create, design, and manage interactive PDF forms using the intuitive form building tools in WPS Office. It simplifies the process with visual drag-and-drop tools.
- 1. Open your PDF in WPS Office: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab on the top ribbon.
- 2. Add form fields visually: Select the type of form field you want to add, such as a Text Field or Check Box, and click on the page to place it exactly where you want.
- 3. Duplicate and align: Right-click the field to duplicate it across multiple rows. Use the built-in alignment tools to ensure all your form rows are perfectly straight without relying on coordinate scripting.
- 4. Customize properties: Double-click any field to open its Properties panel, where you can easily rename the fields sequentially, change their appearance, or add simple actions.

Frequently Asked Questions
Why do my dynamically created PDF form fields share the exact same data?
In PDF forms, fields that have the exact same name are treated as a single group. Inputting data into one will automatically mirror that data into all others with the same name. To fix this, ensure your JavaScript appends a unique, incrementing number to each new field name.
Can I use global JavaScript variables across different PDF sessions?
Standard JavaScript global variables reset when the document is closed. To persist a counter or variable between sessions so that users can return to the form later, you must store the value in a hidden form field or in the document-level metadata.
Is it better to use JavaScript to create PDF fields dynamically or just hide pre-made ones?
For most users and simple forms, pre-creating fields and using a script to toggle their visibility is easier, less prone to layout and coordinate errors, and performs better. Dynamic creation using JavaScript is generally only recommended if the number of rows is highly unpredictable or practically unlimited.




