Avatar of slakin
slakin
 asked on

On a PDF form the calculated field needs to go back to empty when the number is removed from another field.

Very new to javascript.  We have a PDF form that we need to add calculations to one of the columns.

I am trying to set up a calculation in the "Needed" column based on what is entered in the "Completed" column and the static number in the "Required" column.

If the entry in the "Completed" column is more than the "Required" column the "Needed" column should show zero.  If the entry in the "Completed" column is between zero and what is in the "Required", then the "Needed" should subtract the Required from the Completed.  The issue is when someone entered a number in the Completed and then removes it, the "Needed" column keeps the number that is reflected in the "Required".  We would like the "Needed" to be empty (no numbers including no zero).

Everything is working except the part when someone adds a number and then removes it and does not replace it with another number (leaving the "Completed" column empty.

Here is the script we are currently using:


var SchREQ_OthMath = this.getField("SchREQ_OthMath").value;
var CompletedOtherMath = this.getField("CompletedOtherMath").value;
if(CompletedOtherMath >= SchREQ_OthMath)event.value = 0; else
if(CompletedOtherMath !== 0 && CompletedOtherMath < SchREQ_OthMath)
event.value = SchREQ_OthMath - CompletedOtherMath; else
if(CompletedOtherMath == 0)event.value = 0;else
event.value = "";

PDFJavaScript

Avatar of undefined
Last Comment
Kimputer

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kimputer

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
slakin

ASKER
That worked :)   Thank you so much for the help.
slakin

ASKER
Now I am having an issue when a zero is entered in the Completed column nothing shows in the Needed column.  If the completed column is zero then the Needed column should show what is in the Required column.

Kimputer

Strange, your original code should made it be filled in with 0 (which you now say is wrong, but that's besides the point).
So to fix it first to your new specs:

if (CompletedOtherMath >= SchREQ_OthMath) event.value = 0;
else if (CompletedOtherMath !== 0 && CompletedOtherMath < SchREQ_OthMath) event.value = SchREQ_OthMath - CompletedOtherMath;
else if (CompletedOtherMath == 0) event.value =  SchREQ_OthMath;
else event.value = "";
if (CompletedOtherMath == "") event.value = "";

Open in new window

Please check what happens now with entering 0. Maybe remove the last line to test. So test with 0 and test with 0 and deleting 0 (so it's empty). Try with and without the last line of code.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
slakin

ASKER
Testing with 0 (removing the last line of code) - When I entered 0 in the "Completed" column I got the "Required" number to show up in the "Needed" column.  That is what it should do.  But when I delete what is in the "Completed" column, the "Needed" column does not empty out, it remains what was there before the deletion.

Testing with 0 and deleting 0 (keeping in the last line of code) - When I entered 0 in the "Completed" column nothing showed up in the "Needed" column, it should be showing what is in the "Required" column.  But when I delete what is in the "Completed" column, the "Needed" column turns to blank, so that is working right.
Kimputer

Try this:

if (CompletedOtherMath == "") event.value = "";

if (CompletedOtherMath >= SchREQ_OthMath) event.value = 0;
else if (CompletedOtherMath !== 0 && CompletedOtherMath < SchREQ_OthMath) event.value = SchREQ_OthMath - CompletedOtherMath;
else if (CompletedOtherMath == 0) event.value =  SchREQ_OthMath;
else event.value = "

Open in new window