PDF Editing Issues

How to Create Form Rows with Incrementing Field Names in PDF JavaScript

Nimra MalikNimra Malik Nov 5, 2026 868 views

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.

How to Create Form Rows with Incrementing Field Names in PDF JavaScript
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 you start

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.

Solution 1Recommended

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.

1
Create a hidden counter field

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.

2
Set the default value

In the hidden field's properties, set its default value to '0'. This acts as the starting point for your incrementing script.

3
Modify the button script to read the hidden field

Update your 'Add Row' button's JavaScript to retrieve the value from the hidden field using `var i = Number(this.getField('RowCount').value);`.

4
Increment and save the new 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;`.

Store the Row Counter in a Hidden Form Field
Persistent Counters: Using a hidden form field is highly reliable because the counter value is saved directly within the PDF, ensuring the sequence resumes correctly even if the document is closed and reopened later.
Easy PDF Form Builder

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. 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. 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. 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. 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.
Intuitive drag-and-drop PDF form builder without needing complex scripts.Fully compatible with Adobe Acrobat PDF formats and interactive form fields.Easily duplicate and align multiple form fields with built-in alignment tools.Lightweight application with comprehensive PDF editing, converting, and signing features.
microsoft office alternative - wps office

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.