• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Custom Java Script - perform calculation depending on Checkbox value

Community Beginner ,
Jan 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

489

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 13, 2020 Jan 13, 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 Di
...

Votes

Translate

Translate
Community Expert ,
Jan 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

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/1... 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

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

 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

That worked perfectly. Thanks for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

The value of a check-box can't be blank, so the last line is not needed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

Hi Try67,

 

I'm confused, the last line in the above code was not meant to be used as a javascript mouse up action of the checkbox field.

 

This code was meant to be used as custon calculation script of the Total field.

 

The last line  event.value=""; is to trigger a null (empty blank) result in this field if the user doesn't type in anything in the Quantity field.

 

c

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

Sorry, I misread the field name.

At any rate, if it's a calculation script you should use event.value = ..., not this.getField("").value = ...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

LATEST

got it.

 

thank you so much. as usual

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines