Copy link to clipboard
Copied
Hello, I would like to create a conditional equation in Acrobat.
Something like:
If check box # is checked, perform this equation: (Number in field A * number in field B) / 2
And if is not checked, don't divide by 2.
I've tried cobbling together some scripts from different questions to get it to work, but it's just not going for it and I don't know what I'm doing wrong. I've never used JavaScript before and it's not being specific enough to tell me what exactly the problem is. It seems like it doesn't like my else statement, but when I remove it just to get it to go through it acts like there's still an error there EVEN WHEN THERE IS NO LINE 7 ANYMORE.
Baby's first JavaScript is as follows:
// Relevant Field References
var TS = this.getField("Check Box5");
var OG = Number(this.getField("Text3").value);
var CP = Number(this.getField("Text13").value);
var PT = if TS.value = "On") event.value=OG*CP/2;
else event.value=OG*CP
Copy link to clipboard
Copied
Try this:
var TS = this.getField("Check Box5").valueAsString;
var OG = Number(this.getField("Text3").valueAsString);
var CP = Number(this.getField("Text13").valueAsString);
if(TS !== "Off")
event.value = OG*CP/2;
else
event.value = OG*CP;
Copy link to clipboard
Copied
Try this:
var TS = this.getField("Check Box5").valueAsString;
var OG = Number(this.getField("Text3").valueAsString);
var CP = Number(this.getField("Text13").valueAsString);
if(TS !== "Off")
event.value = OG*CP/2;
else
event.value = OG*CP;
Copy link to clipboard
Copied
AAAA IT WORKS THANK YOU
one more thing I forgot about. How do I tell it to round up to the nearest whole number when the answer ends up being something like 2.5?
Copy link to clipboard
Copied
Use Math.round() around the final value.
For example:
event.value = Math.round(event.value);
Copy link to clipboard
Copied
Thanks! I ended up using Math.ciel() to get the rounded up value and it seems to work just as well. Thanks again!

