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

Need help with calculating a value based on the status of a checkbox

Community Beginner ,
Sep 24, 2023 Sep 24, 2023

I'm trying to create a script to provide a value in a text box based on a checkbox and two other text boxes. I've been jumping between a lot of sites trying to figure it out, but no luck.

I'm using the current version of Acrobat Pro.

 

If the checkbox is unchecked, I want to pull the value from "Text Box_1".

If the checkbox is checked, I want to total the values from "Text Box_1" & "Text Box_2".

The results will show in "Text Box_3".

 

Here is what I have, but it doesn't work. Not sure where I'm going wrong.

The checkbox output is the default "Yes".

Text Box_1 & 2 will have a value of something like this: "+1" & "+3", respectively.

The result in Text Box_3 would be either "+1" or "+4" depending on the state of the checkbox.

 

event.value=0;

if(this.getField("Check Box_1").value!="Off") event.value=this.getField("Text Box_1").value;

else if(this.getField("Check Box_1").value!="Yes") event.value=(this.getField("Text Box_1").value + this.getField("Text Box_2").value);

 

Any help would be greatly appreciated.

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
2.4K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 24, 2023 Sep 24, 2023

Use this as 'Custom calculation script' of "Text Box_3" field:

var t1 = Number(this.getField("Text Box_1").valueAsString);
var t2 = Number(this.getField("Text Box_2").valueAsString);
var check = this.getField("Check Box_1").valueAsString;
var total = 0;
total = (check == "Off" ? t1 : t1 + t2);
if(total == 0 || isNaN(total))
event.value = 0;
else
event.value = "+"+total;

 

View solution in original post

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

What exactly isn't working for you?

Are values '1' and '3' or '+1' and '+3'?

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

It won't toggle the value based on the state of the check box.

The values I'm working with (in Text Box 1 & 2) are +1 & +3.

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

I've tried this, too:

if (this.getField("Check Box_1").value!="Off") event.value= this.getField ("Text Box_1").value;

else event.value= this.getField("Text Box_1").value + (this.getField("Text Box_2").value;

 

Not sure what I'm doing wrong. Either argument, used on its own will work fine:

 

This works:  event.value = this.getField("Text Box_1").value

It returns the value of Text Box_1.

 

This works:  event.value = this.getField("Text Box_1").value + this.getField("Text Box_2").value;

It returns the sum of the values of Text Box_1 & 2.

I just can't figure out how to toggle between them based on if the check box is checked or not.

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

This gets me a result of "1" based on the first line (no + sign.)

Checking the checkbox does nothing.

 

event.value= this.getField("Text Box_1").value;
if (this.getField("Check Box_1").value!="Yes") event.value= this.getField("Text Box_1").value + this.getField("Text Box_2").value;

Text Box_1 = "+1"

Text Box_2 = "+2"

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

Use this as 'Custom calculation script' of "Text Box_3" field:

var t1 = Number(this.getField("Text Box_1").valueAsString);
var t2 = Number(this.getField("Text Box_2").valueAsString);
var check = this.getField("Check Box_1").valueAsString;
var total = 0;
total = (check == "Off" ? t1 : t1 + t2);
if(total == 0 || isNaN(total))
event.value = 0;
else
event.value = "+"+total;

 

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

Yes! You are awesome! Thank you so much for your help with this. It was driving me nuts.

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

I am greatly appreciative of you solving this for me. I was wondering if you might be able to answer one more question for me?

The values in Text Box 1 & 2 will change over time. Is there a way to have Text Box 3 automatically update the total when they do?

Using your script, I am able to update it manually by unchecking and re-checking Check Box 1.

Thank you for all of your help.

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

If you use script as 'Custom calculation script' of "Text Box_3" as I told you in my post, it will auto update result.

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

Strange. I have it entered in the Custom Calculation box (that's where I've been trying all the iterations) but it won't auto-update the total. Only if I uncheck and re-check.

Scott26850993p6yn_0-1695755434712.png

 

Translate
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 ,
Sep 26, 2023 Sep 26, 2023
LATEST

I figured it out. I had to change the fields calculation order to put the checkbox after the text boxes.

Thank you again for all of your help on this. Wouldn't have gotten there without you.

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

Where does you use the script?

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

Thank you for reaching out. Nesa was able to solve it for me.

Translate
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