How to Populate a Text Field with Checked Checkbox Values in PDF Forms
Question details
The user wants to write a JavaScript function to automatically list the values of checked checkboxes inside a single multiline text box within a PDF form.

- Product
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms where a text field dynamically displays the specific options a user has selected via checkboxes.
- Observed behavior
- A multiline text field needs to be populated with the specific string values of checkboxes, separated by line breaks, only when those checkboxes are checked.
Ensure you are using a PDF editor that supports form field creation and custom JavaScript calculations, and verify that your checkboxes have designated export values.
Configure a Custom JavaScript Calculation in the Text Field
Set the destination text field to accept multiple lines of text and apply a custom JavaScript calculation script to dynamically retrieve and display the checked values.
Interactive PDF forms can use JavaScript to pass data between fields. By assigning a custom calculation script to the target text field, the form will automatically check the status of your checkboxes every time the user interacts with them, updating the text box accordingly.
Open your PDF form editor, right-click the target text field, and select 'Properties'. Navigate to the 'Options' tab and check the 'Multiline' box to ensure the field can display line breaks.
Switch to the 'Calculate' tab within the same text field properties window. Select the option labeled 'Custom calculation script'.
Click the 'Edit' button and paste the following script: `var c1 = this.getField('Checkbox 1').valueAsString; var c2 = this.getField('Checkbox 2').valueAsString; if(c1 !== 'Off' && c2 !== 'Off') { event.value = c1 + '\n' + c2; } else { event.value = ''; }`. Be sure to change 'Checkbox 1' and 'Checkbox 2' to the exact names of your actual checkbox fields.
Click 'OK' to save the script and close the properties dialog. Exit form editing mode and test your document by clicking the checkboxes to verify that the text field populates with the correct values.

Easily Manage Interactive PDF Forms with WPS PDF
WPS Office provides a comprehensive built-in PDF editor that allows you to view, fill, and manage interactive PDF forms without hassle. Whether you are dealing with checkboxes, text fields, or digital signatures, WPS PDF makes document handling seamless.
- 1. Open WPS Office: Launch WPS Office on your device and select the 'PDF' tab from the home screen.
- 2. Load Your PDF Form: Drag and drop your interactive PDF file into the workspace, or click 'Open' to browse your local files.
- 3. Interact with Form Fields: Simply click on the checkboxes or text fields within the document. WPS PDF automatically recognizes interactive elements, allowing you to fill out the form efficiently.

Frequently Asked Questions
Why is the text field not displaying multiple lines even though I added the line break character?
The text field must have the 'Multiline' property explicitly enabled in its settings. Without this property checked, the newline character ('\n') will be ignored, and the text will be forced onto a single line.
How can I change the export value of a checkbox in a PDF form?
In your PDF editor's form preparation mode, right-click the checkbox and select 'Properties'. Go to the 'Options' tab, where you can define the 'Export Value' (e.g., 'Yes', 'Selected', or a specific category name). This is the exact string the JavaScript will retrieve when the box is checked.
Can I hide the text field if no checkboxes are selected?
Yes. You can add logic to your calculation script that checks if all checkbox values return 'Off'. If they do, you can set the text field's display property to hidden using `event.target.display = display.hidden;`, and revert it to `display.visible` when a box is checked.




