How to Hide a PDF Form Field Using JavaScript When Value is Less Than 2
Question details
The user needs a JavaScript solution for a PDF form to calculate the days between two dates and hide the result field if the calculated number is less than two.

- Product
- PDF Form Scripting
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form that calculates date differences and conditionally hides fields based on the result.
- Observed behavior
- The script fails to hide the field properly because it incorrectly assigns values to event.target instead of event.value, and attempts to compare a string instead of a numeric calculation.
Ensure your PDF editor supports JavaScript for forms and that you have correctly identified the field names for both the start and end dates in your document.
Fix the JavaScript Logic to Compare Numeric Values
Adjust the calculation script to use event.value instead of event.target, and compare the raw numeric calculation instead of the string output.
A common mistake in PDF form scripting is trying to evaluate a string as a number. Once you append text like 'days' to your numeric calculation, the value becomes a string. To fix this, you must run the less-than comparison on the mathematical output before it converts to a string format.
Open your PDF form in editing mode, right-click the target field, select Properties, and navigate to the Calculate tab where your custom JavaScript is located.
Locate the line assigning the value. Change 'event.target = Math.round(eDays) + " days";' to 'event.value = Math.round(eDays) + " days";'. You should always use event.value to assign the calculated result.
Update the conditional statement to compare the numeric value before the text is added. Use 'if (Math.round(eDays) < 2)' instead of comparing the event.target.value.
Add a condition to handle blank completion dates to prevent the script from returning errors or hiding fields prematurely. Update your condition to include '|| sDate2 == ""'.

Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a comprehensive suite for creating, editing, and managing interactive PDF forms. Whether you need to insert form fields, adjust properties, or handle basic form parameters, WPS PDF makes the process straightforward.
- 1. Open PDF in WPS: Launch WPS Office and open your target PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top menu bar to access the interactive form tools.
- 3. Insert Form Fields: Select the desired form field type, such as Text Field or Checkbox, and draw it onto your document.
- 4. Adjust Properties: Right-click the newly created field and select 'Properties' to adjust formatting, options, and actions.

Frequently Asked Questions
Why isn't my PDF form field calculation updating automatically?
Calculations in PDF forms rely on the calculation order. If multiple fields depend on one another, ensure the calculation order is set correctly in your PDF editor's form settings.
What is the difference between event.value and event.target in PDF JavaScript?
event.value refers to the output value of the current field triggering the calculation script, while event.target refers to the field object itself. Always use event.value to assign calculation results.
How do I hide a form field completely in a PDF?
You can hide a form field by changing its display property in the field properties menu to 'Hidden', or by using JavaScript such as 'event.target.display = display.hidden;'.




