How to Conditionally Underline a PDF Text Field Using a Radio Button
Question details
The user needs to conditionally underline text in a PDF form field when a specific 'Yes' radio button is selected, but their current JavaScript implementation is not working.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that automatically updates the text formatting of a field based on a user's radio button selection.
- Observed behavior
- The current script utilizing rich-text formatting fails to underline the text field when the radio button state changes.
Ensure you are using a professional PDF editor with form creation and JavaScript capabilities, and verify that the target text field has 'Allow Rich Text Formatting' explicitly enabled in its properties.
Configure JavaScript on the Radio Button Action
Trigger the rich-text formatting update directly when the user clicks the Yes or No radio button.
For JavaScript to dynamically change text formatting in a PDF, the target text field must be configured to accept rich text. You can then attach an action script to the radio button that modifies the richValue spans of the text field.
Right-click the target text field, select 'Properties', navigate to the 'Options' tab, and check the box for 'Allow Rich Text Formatting'.
Right-click the Yes/No radio button, choose 'Properties', and navigate to the 'Actions' tab.
Select 'Mouse Up' from the 'Select Trigger' dropdown, choose 'Run a JavaScript' from the 'Select Action' dropdown, and click 'Add'.
Input a script that checks the value of the radio button. If the value is 'Yes', use the script to set the target field's richValue array with {textDecoration: 'underline'}. Ensure you include an 'else' statement to restore the normal formatting if 'No' is selected.

Apply Formatting Script Directly to the Text Field
Use the text field's own formatting properties to listen to the radio button state and apply the underline.
Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides comprehensive PDF editing tools, allowing you to easily create forms, add interactive fields, and manage properties without dealing with complex coding. Fully compatible with standard PDF formats.
- 1. Open PDF File: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to access interactive elements.
- 3. Insert Form Fields: Use the 'Text Field' and 'Radio Button' tools to insert interactive elements onto your page.
- 4. Adjust Properties: Double-click the inserted fields to adjust properties like text formatting, field names, and export values.

Frequently Asked Questions
Why isn't my PDF JavaScript applying the underline formatting?
For rich-text scripts to work in a PDF text field, you must explicitly check the 'Allow Rich Text Formatting' option located in the text field's 'Options' tab. Without this enabled, scripts modifying richValue arrays will be ignored.
Can I apply other text formats conditionally using JavaScript?
Yes. By modifying the richValue object in your JavaScript, you can dynamically change font color, font weight (bold), italic styling, and text size based on user interactions.
How do I remove the underline if the user changes their selection to 'No'?
Your JavaScript logic must include an 'else' block. If the 'Yes' condition sets the textDecoration property to 'underline', the 'else' condition should set it to 'none' to revert the formatting when 'No' is clicked.




