Skip to main content
Participating Frequently
January 24, 2022
Question

Checkbox triggers specific variable to be used in calculation?

  • January 24, 2022
  • 2 replies
  • 1279 views

Hi there, 

I'm creating a PDF septic inspection report form that automatically calculates elevations.

 

I want to use checkboxes (or radio buttons) to change the variable used to subtract from:

ie: 

Without checkbox checked, X-Z=N (or HIBenchkmark-FSBldg Sewer = ElevBldg Sewer)

With checkbox checked, Y-Z=N (or TPHI-FSBldg Sewer = ElevBldg Sewer)

 

I'm fairly new-ish to javascript, and would appreciate any and all help on this topic!

I've also attached the form that I am working on with current calculations. 

This topic has been closed for replies.

2 replies

Nesa Nurani
Community Expert
Community Expert
January 25, 2022

Try this:

var v1 = Number(this.getField("HIBenchmark").value);
var v2 = Number(this.getField("FSBldg Sewer").valueAsString);
var v3 = Number(this.getField("TPHI").value);
var check = this.getField("Check Box TP?1").valueAsString;

if(v2 == "")
event.value = "";
else if (check != "Off" )
event.value = v3-v2;
else
event.value = v1-v2;

Participating Frequently
January 25, 2022

Thanks for you help, I really appreciate it!

 

I tried this code, but it's saying that there is a SyntaxError: unterminated string literal 4: at line 5. It's also highlighting "if(v2 == ""). 

Nesa Nurani
Community Expert
Community Expert
January 25, 2022

Did you copy entire script?

Here is your file with script in it:

https://drive.google.com/uc?export=download&id=1nFTAPgXS7gI-dnmO6hfzxuc9ZtnMVhVg 

Participating Frequently
January 24, 2022

For example, i'm trying a code like this:

 

// Get first field value

var v1 = getField("HIBenchmark").value;

// Get second field value

var v2 = getField("FSBldg Sewer").value;

// Get third field value

var v3 = getField("TPHI").value;

 

if(this.getField("Check Box TP?1").isBoxChecked = true))

// Set this field value equal to the difference

event.value = v3 - v2;

 

else(this.getField("Check Box TP?1).isBoxChecked = false

// Set this field value equal to the difference

event.value = v1 - v2;

 

But it says that there's an error at the first "//Set this field value"

Participating Frequently
January 24, 2022

made some mistakes above, this is what i'm working with: 

 

// Get first field value

var v1 = getField("HIBenchmark").value;

// Get second field value

var v2 = getField("FSBldg Sewer").value;

// Get third field value

var v3 = getField("TPHI").value;

 

if(v3>.01){

this.getField("Check Box TP?1").isBoxChecked = true))

// Set this field value equal to the difference

event.value = v3 - v2;

 

}else{

this.getField("Check Box TP?1).isBoxChecked = false))

// Set this field value equal to the difference

event.value = v1 - v2;

}