Copy link to clipboard
Copied
I'm using Acrobat DC Pro.
I am trying to show or hide a field based on the calculated total that appears in a field named NVCA Raw Score. The calculated field has possible numbers of 0 - 7. If the calculated field displays number 0 - 4, I want to hide the FLAGGED field. If the calculated fields displays numbers 5, 6, or 7, I want the hidden field to display. I've tried my best to cobble some JavaScript together from other scripts using < >, but the hidden field never displays. I've learned a lot from this group, but still need help every once in a while. Any help will be very appreciated.
var nResult = this.getField("NVCA Raw Score").value;
if ( nResult >= 4 ) {
this.getField("FLAGGED").display = display.visible;
}
else if ( nResult <= 4 ) {
this.getField("FLAGGED").display = display.hidden;
}
Many thanks,
Charlie
charlie6067
A validation script doesn't make sense in this context since it only gets triggered when the value of that field is changed (by the user!), and this is a read-only field, so it never happens.
Move the code to be a custom calculation script and change it to:
var nFlag = Number(this.getField("NVCA Raw Score").value);
if ( nFlag >= 5 ) {
event.target.display = display.visible;
} else {
event.target.display = display.hidden;
}
Copy link to clipboard
Copied
Where do you put the script?
Copy link to clipboard
Copied
There is nothing visibly wrong with your code. To figure out what's going wrong I'd start off by running it from the console window. I almost always develop code in the console window first, to make sure it does what I think it does. If you can change the switching value and then run the code and see the field hide/show, then you know the code is correct, but isn't getting run at the right time.
So yes, script location is very important.
The second thing I'd do is place a "console.println()" statement in the code so you can watch the Console window know exactly when the code is (or isn't) executed. And tell what value (nResult) is being used.
Copy link to clipboard
Copied
There is one problem with your code. Both conditions cover the scenario where the value of the field is exactly 4. That is not a desired situation. You need to decide what should happen in that case and remove the "=" part from the other condition.
Copy link to clipboard
Copied
Thom and others - Thank you for your replies. I've still not been able to figure out why the JavaScript won't show or hide the Flagged field based on the score in another field. I've attached the part of the form I'm having problems with. If you would please take a minute to look at the validate code, you may quickly spot what I've failed to notice. I've tried the same code in a JavaScript module but still can't get it to show/hide the field.
Thanks,
Charlie
Public link https://www.dropbox.com/s/47cjaoyxbiefgms/Example.pdf?dl=0
Copy link to clipboard
Copied
A validation script doesn't make sense in this context since it only gets triggered when the value of that field is changed (by the user!), and this is a read-only field, so it never happens.
Move the code to be a custom calculation script and change it to:
var nFlag = Number(this.getField("NVCA Raw Score").value);
if ( nFlag >= 5 ) {
event.target.display = display.visible;
} else {
event.target.display = display.hidden;
}
Copy link to clipboard
Copied
It works. Thank you very much for your time and explanation.
Charlie
Find more inspiration, events, and resources on the new Adobe Community
Explore Now