Copy link to clipboard
Copied
I know I've seen this somewhere else in this forum, but I can't find it.
I have a number field that is calculated based on the value of two other fields. However, when the other fields are empty or the calculation otherwise equals zero, I want to hide the form field so that the zero is not visible. I think I've got the code right, but it doesn't seem to work. This is the code I'm using:
var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;
if ( total!=0 )
{
event.target.value = total;
event.target.display = display.visible;
}
else
{
event.target.display = display.hidden;
}
This code is used on the calculations for the field I want to hide. All three fields are specified as number fields. What am I missing?
Edited to add: The field I want to hide is used in other fields and calculations, so I want to keep the zero value, but I want the field to appear blank if the value is zero. This is so the form can be printed and used manually.
Thanks in advance for any suggestions!
Copy link to clipboard
Copied
It appears I have answered my own question. I've been struggling with this for hours, and after posting it here, the solution became clear. I simply switched the if/else statements. Instead of testing for when the field does not equal zero, I tested for when it does equal zero, and it hid everything correctly. Here's my updated code, in case it helps someone else.
var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;
if ( total==0 )
{
event.target.display = display.hidden;
}
else
{
event.target.display = display.visible;
event.target.value = total;
}
Copy link to clipboard
Copied
The value needs to be set with event.value.
event.value = total;
Otherwise your script looks correct.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
It seems to work with event.target.value, but I will change to event.value if that is more correct. Thanks for your help!
Copy link to clipboard
Copied
It appears I have answered my own question. I've been struggling with this for hours, and after posting it here, the solution became clear. I simply switched the if/else statements. Instead of testing for when the field does not equal zero, I tested for when it does equal zero, and it hid everything correctly. Here's my updated code, in case it helps someone else.
var field1 = this.getField("Field 1").value;
var field2 = this.getField("Field 2").value;
var total = field1 * field2;
if ( total==0 )
{
event.target.display = display.hidden;
}
else
{
event.target.display = display.visible;
event.target.value = total;
}
Copy link to clipboard
Copied
That's rediculous. It literally makes no difference.
And for event.value, it's more important than you think that the values are set correctly. There is a specific order to events on the fields. The event.value property sets the value from the inside of the calcuation event. The event.target.value property references the field from the outside, it throws the event engine off, and triggers more events. This can cause problems with field formatting, other scripts, and slow everything down.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
I want to hide the form field so that the zero is not visible
You don't need to hide the field, just hide the value, without removing it so further calculations can occurs.
Place this as a Custom Format script in the "total" field:
if (event.value == 0) {event.value = ""};
Acrobate du PDF, InDesigner et Photoshopographe
Copy link to clipboard
Copied
Can you (or anyone else that could) help me to amend the above (or below code) to include negative numbers as well? I wish to hide "0" and any negative numbers.
if (event.value == 0) {event.value = ""};
Copy link to clipboard
Copied
if (event.value <= 0) {event.value = ""};
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you!!! Be Well!!
Copy link to clipboard
Copied
Can I add code to the above script to show two decimals when the field is not blank? Such as 19.50 instead of 19 for the result.
Copy link to clipboard
Copied
if (event.value <= 0) event.value = "";
else event.value = util.printf("%0.2f",event.value);
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hi! Is it possible to create a script that will hide a field value depending on the scenario?
For example, I'm creating a table that calculates average hours worked. In some scenarios, hours will only be entered in the 'Week 1 Hours' row. However, in other scenarios, hours will be entered in both 'Week 1 Hours' and 'Week 2 Hours' rows.
I'm wondering if it's possible to hide the value of the "Avg Total Hours" field IF no hours are entered in the 'Week 2 Hours' row? Essentially, I only want the average value to be visible if there are values in both the 'Total1' and 'Total2' fields.
Copy link to clipboard
Copied
Why Yes, it is possible with a script. Best to use the calculation script in either Total2 (sinces it's row is the trigger) or in the Avg Total Hours field.
Read this article:
https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
It might be simpler to just show a blank value when the output is zero. This can be done with a custom Format script. Format scripts do not chang ethe value of a field, they just change the display of a value.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you so much for sharing! I am completely ignorant when it comes to code and your simple line worked for what I need!
Copy link to clipboard
Copied
This works great, and so simple. thank you!

