Skip to main content
Participating Frequently
September 24, 2023
Answered

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

  • September 24, 2023
  • 2 replies
  • 2255 views

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.

This topic has been closed for replies.
Correct answer Nesa Nurani

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"


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;

 

2 replies

Bernd Alheit
Community Expert
Community Expert
September 25, 2023

Where does you use the script?

Participating Frequently
September 26, 2023

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

Nesa Nurani
Community Expert
Community Expert
September 24, 2023

What exactly isn't working for you?

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

Participating Frequently
September 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.

Participating Frequently
September 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.