Skip to main content
Inspiring
January 10, 2020
Answered

Use formatting display to hide number form fields with value of zero

  • January 10, 2020
  • 3 replies
  • 9485 views

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!

This topic has been closed for replies.
Correct answer tcarano65

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;
}

 

3 replies

JR Boulay
Community Expert
Community Expert
January 10, 2020

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
tacnola
Known Participant
April 9, 2020

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 = ""};

Thom Parker
Community Expert
Community Expert
April 10, 2020

if (event.value <= 0) {event.value = ""};

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
tcarano65AuthorCorrect answer
Inspiring
January 10, 2020

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;
}

 

Thom Parker
Community Expert
Community Expert
January 10, 2020

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. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
January 10, 2020

The value needs to be set with event.value. 

 

event.value = total;

 

Otherwise your script looks correct. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
tcarano65Author
Inspiring
January 10, 2020

It seems to work with event.target.value, but I will change to event.value if that is more correct. Thanks for your help!