PDF Editing Issues

How to Increment and Decrement Ordinal Numbers in PDF Forms Using JavaScript

Natalie TaylorNatalie Taylor Nov 3, 2026 869 views

Question details

The user needs to create interactive buttons in a PDF form that increment or decrement a text field's numerical value while automatically applying the correct ordinal suffix (e.g., 1st, 2nd, 3rd, 4th).

How to Increment and Decrement Ordinal Numbers in PDF Forms Using JavaScript
Product
PDF
Device & OS
not provided
Scenario
Developing interactive PDF forms with dynamic counter fields that respond to user clicks.
Observed behavior
The user wants a text field's value to change sequentially with ordinal indicators (from 1st to 2nd to 3rd) upon button clicks, but is encountering syntax and logic issues when reading and updating the field value.
Before you start

Ensure you are using a PDF editor that supports document-level JavaScript and interactive form fields. Familiarity with basic JavaScript syntax and form field properties is highly recommended before writing the script.

Solution 1Recommended

Use Document-Level Scripts and Button Actions

Create a centralized JavaScript function to handle the math and suffix logic, then call these functions from your interactive buttons to update the field value dynamically.

Instead of writing the entire script directly into the button, it is best practice to place your calculation and formatting logic in a document-level script. This allows multiple buttons to call the same functions.

When reading the field, ensure you are accessing its value (using valueAsString) rather than the field object itself.

1
Set up the target text field

Add a text field to your PDF form and name it (e.g., 'OrdinalCounter'). Open its properties and set its default value to '1st'.

2
Add document-level JavaScript

Navigate to your PDF editor's JavaScript console or Document Scripts section. Create a new document-level script to house your functions.

3
Write the calculation logic

Create countUp() and countDown() functions. In these functions, use getField('OrdinalCounter').valueAsString to read the current value. Use string manipulation to strip the current suffix (st, nd, rd, th) and convert the remaining string into a numeric value, then add or subtract 1.

4
Apply the ordinal formatter

Write a formatting function to append the correct suffix to the new number. Check if the last two digits are 11, 12, or 13, as these always take 'th'. For other numbers, append 'st' if it ends in 1, 'nd' if 2, 'rd' if 3, and 'th' for all others.

5
Configure the button actions

Create 'Up' and 'Down' button fields. Right-click to access their Properties, navigate to the Actions tab, choose 'Mouse Up' as the trigger, and select 'Run a JavaScript' as the action. Enter countUp(); for the up button and countDown(); for the down button.

Use Document-Level Scripts and Button Actions
Handling Exceptions: Numbers ending in 11, 12, and 13 are exceptions to the standard ordinal rules. Make sure your JavaScript explicitly checks for these using modulo math (e.g., num % 100) before applying the standard suffix logic.

Edit and Manage Interactive PDF Forms with WPS Office

WPS PDF provides comprehensive tools to create, edit, and fill interactive PDF forms. Whether you need to insert text fields, checkboxes, or buttons, WPS Office makes it simple to manage your document workflows efficiently.

  1. 1. Open your PDF: Launch WPS Office, select the PDF module, and open your document.
  2. 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive form toolkit.
  3. 3. Add or modify fields: Use the available tools to insert text fields, push buttons, or modify existing form elements.
  4. 4. Configure properties: Double-click any form field to open its properties panel, where you can set default values and basic actions.
Supports viewing and filling interactive PDF forms seamlessly.Highly compatible with standard Adobe Acrobat PDF formats and interactive elements.Includes intuitive tools to insert text fields, buttons, and checkboxes.Lightweight, fast processing for large and complex PDF files.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my JavaScript returning an object error instead of the text value?

This happens when you reference the field object itself rather than its contents. Make sure you use getField('FieldName').value or getField('FieldName').valueAsString to extract the actual text, rather than just getField('FieldName').

How do I mathematically check for 11th, 12th, and 13th in JavaScript?

You can use the modulo operator. First, find the remainder of the number divided by 100 (num % 100). If the result is 11, 12, or 13, append 'th'. If not, find the remainder of the number divided by 10 (num % 10) to determine if it should end in 'st', 'nd', 'rd', or 'th'.

Can I use the same ordinal script for multiple fields in one PDF?

Yes. Instead of hardcoding the field name into your document-level script, modify your countUp() and countDown() functions to accept a parameter (e.g., function countUp(fieldName)). Then, in each button's action, pass the target field's name as an argument like countUp('Counter1');.