How to Hide a PDF Form Title When a Field is Blank using JavaScript
Question details
The user needs a PDF form title to remain visible when an optional field contains text, and hidden from printing when the field is empty.

- Product
- PDF Form Editors
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms where certain titles or labels should only print if their corresponding input fields are filled out.
- Observed behavior
- Using the valueAsString property prevents the title field from updating correctly after an existing value is deleted from the input field.
Ensure you are using a PDF editor that supports advanced form field editing and JavaScript execution, and verify the exact field names of both your input field and your title field.
Use the Validation Event Value to Control Display Properties
Replace the incorrect valueAsString property with event.value during the field validation event to ensure the display properly updates when text is deleted.
The PDF JavaScript 'event' object does not natively support a 'valueAsString' property in this context, which causes the script to fail when a previously entered value is cleared. By capturing 'event.value' during the field's validation trigger, you can reliably evaluate whether the field is blank and toggle the target title field's visibility accordingly.
Open your PDF in form editing mode, right-click the optional input field that users will fill out, and select 'Properties'.
Go to the 'Validate' tab within the properties window, select the option to 'Run custom validation script', and click the 'Edit' button.
Input the script to check the event value: 'var rrt = event.value;'. Then, use the 'this.getField("YourTitleFieldName")' method to target your title field.
Add a conditional statement: if 'rrt' is empty, set the title field's display property to 'display.noPrint' (or 'display.hidden'). If it contains a value, set it to 'display.visible'.

Create and Manage Dynamic PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to easily insert text fields, manage form properties, and handle document formatting without complex scripting.
- 1. Open your PDF in WPS: Launch WPS Office and open your existing PDF document.
- 2. Access the Form Tab: Navigate to the 'Form' tab on the top ribbon to reveal all interactive form creation tools.
- 3. Edit Field Properties: Double-click any form field to open its properties panel, where you can directly adjust its appearance, visibility, and basic actions.

Frequently Asked Questions
Why isn't my PDF JavaScript updating when I delete text from a field?
This typically happens if you use incorrect event properties like 'valueAsString'. Using 'event.value' in a validation script ensures the program accurately registers when a field becomes completely blank.
What is the difference between display.hidden and display.noPrint in PDF form fields?
'display.hidden' makes the field completely invisible on the screen and prevents it from printing. 'display.noPrint' keeps the field visible on the screen for the user while filling out the form, but hides it when the document is printed.
Can I hide multiple titles or fields based on a single input field?
Yes. In your custom validation script, you can reference multiple field names using 'this.getField()' and apply the same display property changes to each of them within your conditional statement.




