PDF Editing Issues

How to Count Selected Items in a PDF List Box with JavaScript

Aamir Naveed AkramAamir Naveed Akram Nov 3, 2026 869 views

Question details

Display the total number of selected entries from a multi-select PDF list box in a separate text field using JavaScript, while ensuring individual export values remain unchanged.

How to Count Selected Items in a PDF List Box with JavaScript
Product
PDF
Device & OS
not provided
Scenario
Developing or editing an interactive multi-select PDF form where an automated count of user selections is needed.
Observed behavior
The separate text field calculates and displays an integer representing the exact number of selected list box items after the user commits their selection.
Before you start

Ensure you have access to a PDF editor that supports custom form field creation and JavaScript execution, such as Adobe Acrobat Pro.

Solution 1Recommended

Use Custom JavaScript in the Text Field Calculation

Apply a custom calculation script to the target text field to evaluate the currentValueIndices array of the list box.

The most reliable method to count selections without touching export values is to use the currentValueIndices property of the list box. This property returns either a single number (if one item is selected) or an array of numbers (if multiple items are selected).

1
Open Text Field Properties

Right-click the destination text field where you want the count to appear, select 'Properties', and navigate to the 'Calculate' tab.

2
Enable Custom Calculation

Select the 'Custom calculation script' option at the bottom of the tab, then click the 'Edit' button.

3
Enter the Script

Input the following JavaScript (assuming your list box is named 'listbox1'): var selections = this.getField("listbox1").currentValueIndices; event.value = (typeof selections == "number") ? ((selections < 0) ? 0 : 1) : selections.length;

4
Save and Test

Click 'OK' to close the JavaScript editor, close the Properties window, and test your list box to ensure the count updates correctly.

Use Custom JavaScript in the Text Field Calculation
Script Execution Timing: The calculation script typically runs only after the user commits their selection in the list box, such as by clicking outside the box or tabbing to the next field.
Powerful PDF Form Editor

Easily Edit and Manage PDF Forms with WPS Office

WPS PDF offers comprehensive tools to view, fill, and edit interactive PDF forms. Whether you need to manage text fields, checkboxes, or interact with list boxes, WPS PDF provides an intuitive interface to handle your PDF documents efficiently.

  1. 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and install it on your computer.
  2. 2. Open Your PDF Form: Launch WPS PDF and open the interactive form document you want to work on.
  3. 3. Fill and Manage Form Fields: Use the built-in form tools to easily interact with list boxes, text fields, and calculate values seamlessly.
Seamlessly compatible with standard PDF forms and Adobe Acrobat formats.Lightweight and fast, opening complex interactive PDFs in seconds.Built-in tools for filling forms, adding signatures, and modifying text fields.Free to download with a highly intuitive, user-friendly interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my text field not updating immediately when I click a list box item?

JavaScript calculations in standard PDF forms generally trigger when a field loses focus or the value is officially committed. You must usually click outside the list box or press 'Tab' for the text field to update its count.

Can I use this JavaScript if my list box only allows single selections?

Yes. The provided script checks if the currentValueIndices property returns a number (indicating a single selection) or an array (indicating multiple selections), handling both gracefully and returning 1 for a single valid selection.

Does modifying the list box export values affect this JavaScript count?

No. Using currentValueIndices relies on the physical index positions of the selected items in the list, not their specific text labels or export values. This makes the script highly robust against underlying data changes.

Will this custom JavaScript execute properly in all PDF readers?

While it works perfectly in robust PDF editors and desktop applications, some browser-based or basic mobile PDF viewers do not support JavaScript execution. For full functionality, users should open the form in a fully-featured PDF reader.