Use Acrobat Buttons to Fill Sequential PDF Form Fields
Question details
Set up Heads and Tails buttons that record values in sequential PDF form fields without overwriting earlier entries.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form has sequential toss fields and two buttons for recording Heads or Tails.
- Observed behavior
- Each button should fill the first empty field with its assigned value and display an alert when all fields are full.
Open the PDF in Adobe Acrobat and check that it has text fields named toss1 through toss5, plus Heads and Tails buttons. Make sure the fields start empty and Acrobat JavaScript is enabled in Preferences.
Add a Mouse Up JavaScript action to each Acrobat button
Use a script to find the first empty toss field, record the selected result, and stop when all five fields are filled.
In Acrobat, open the PDF and choose Prepare a Form from the tools menu. Create or rename the text fields toss1 through toss5 and add Heads and Tails buttons if needed.
Open the Heads button's Properties and select the Actions tab. Set Select Trigger to Mouse Up and Select Action to Run a JavaScript, then click Add.
In the JavaScript editor, enter the following and click OK: for (var i = 1; i <= 5; i++) { if (this.getField("toss" + i).valueAsString === "") { this.getField("toss" + i).value = "1"; break; } } if (i > 5) app.alert("You cannot perform another toss.", 1);
Open the Tails button's Properties and add a Mouse Up action using Run a JavaScript. Paste the same script, but change the assigned value from "1" to "0".
Save the PDF, exit Prepare a Form, and click Heads and Tails in different sequences. Confirm that each click fills the next empty field, earlier entries remain unchanged, and a sixth click displays the alert.

Work with PDF Forms and Office Files in WPS Office
WPS Office is a free, lightweight, easy-to-use alternative for everyday PDF and Office work. Its familiar interface and format compatibility make it easier to move between existing files; use Acrobat to create and test the button scripts described above.

Frequently Asked Questions
Why does the script use the first empty field?
The Mouse Up script checks toss1 through toss5 in order and writes to the first field whose valueAsString is empty. The break statement stops it from changing later fields.
How do I add more than five toss fields?
In Prepare a Form, add fields with consecutive names such as toss6 and toss7. In each button's script, change both occurrences of 5 to the new total.
What happens when every toss field is filled?
The loop finds no empty field, so the script displays “You cannot perform another toss.” No existing entry is changed.
Why isn't my button script working?
In Acrobat, check the button's Properties > Actions tab for a Mouse Up trigger and Run a JavaScript action. Verify the field names and JavaScript preference; on Windows, press Ctrl+J to inspect the JavaScript Console.
Can I use a blank value for one of the results?
No. The script treats a blank field as unused, so a blank result could be overwritten. Assign a nonblank value such as 0, 1, or a dash instead.




