Skip to main content
Participating Frequently
January 13, 2020
Answered

Custom Java Script - perform calculation depending on Checkbox value

  • January 13, 2020
  • 2 replies
  • 879 views

Hello,

I have a form that a user will select if a specific item is for a member or non member (with associated price discount for members). They also have to enter a Quantity. There is then a total amount field that gets automatically calculated. I have part of this working but would like the Total amount field to go back to being null if the user unchecks the Quantity and Member or Nonmember checkbox. Here is the code that I currently have:

if (this.getField("511 Digital Access Code Ckbx").valueAsString == "No") event.value=(this.getField("511 Digital Access Code Qty").value * 40);
if (this.getField("511 Digital Access Code Ckbx").valueAsString == "Yes") event.value=(this.getField("511 Digital Access Code Qty").value * 20);
if (this.getField("511 Digital Access Code Qty").value == null) event.value=(this.getField("511 Digital Access Code Total").value == null);
if (this.getField("511 Digital Access Code Qty").rawValue.length == 0) event.value=(this.getField("511 Digital Access Code Total").value == null);

 

example of issue. User selects member pricing ($20) for an item with the Quanity of 5 . The Total field calculates to $100. That all works fine. If they unselect the checkbox and clear out the Quantity field, I still have a total of $100 in the Total field.

This topic has been closed for replies.
Correct answer ls_rbls

If you were able to review the little script that I shared in the link above, you will notice that I'm not using "Yes" and "No" to set the condition that I used in the calculation script.

 

You need to use "Yes" and/or "Off" instead of "Yes" and/or "No" to reflect the correct operation in your calculation when the  checkbox is ticked or un-ticked.

 

If you want to see for yourself create really quick a text field, and add a custom calculation  script like this: 

 

 

event.value = this.getField(""511 Digital Access Code Ckbx").value;

 

 

Try the following code for your custom calculation script of the "511 Digital Access Code Total" field:

 

if (this.getField("511 Digital Access Code Ckbx").value == "Off") event.value=(this.getField("511 Digital Access Code Qty").value * 40);

if (this.getField("511 Digital Access Code Ckbx").value == "Yes") event.value=(this.getField("511 Digital Access Code Qty").value * 20);

if (this.getField("511 Digital Access Code Qty").value =="") event.value ="";

 

 

 

 

 

2 replies

ls_rbls
Community Expert
ls_rblsCommunity ExpertCorrect answer
Community Expert
January 14, 2020

If you were able to review the little script that I shared in the link above, you will notice that I'm not using "Yes" and "No" to set the condition that I used in the calculation script.

 

You need to use "Yes" and/or "Off" instead of "Yes" and/or "No" to reflect the correct operation in your calculation when the  checkbox is ticked or un-ticked.

 

If you want to see for yourself create really quick a text field, and add a custom calculation  script like this: 

 

 

event.value = this.getField(""511 Digital Access Code Ckbx").value;

 

 

Try the following code for your custom calculation script of the "511 Digital Access Code Total" field:

 

if (this.getField("511 Digital Access Code Ckbx").value == "Off") event.value=(this.getField("511 Digital Access Code Qty").value * 40);

if (this.getField("511 Digital Access Code Ckbx").value == "Yes") event.value=(this.getField("511 Digital Access Code Qty").value * 20);

if (this.getField("511 Digital Access Code Qty").value =="") event.value ="";

 

 

 

 

 

Participating Frequently
January 14, 2020

That worked perfectly. Thanks for your help.

ls_rbls
Community Expert
Community Expert
January 14, 2020

You're welcome. Happy to help.

 

Don't forget to mark this solution as correct, so that other users with similar issues can find the answer quickly in the forums

ls_rbls
Community Expert
Community Expert
January 13, 2020

Hi,

 

Just yesterday I shared with another user a little script that just do exactly the opposite, but you can modify it to suite your script action.

 

You can review it here:

https://community.adobe.com/t5/acrobat/display-error-message-on-checkbox-if-drop-down-selected/m-p/10853774#M235965