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

Conditional Equation based on checkbox

New Here ,
Mar 07, 2025 Mar 07, 2025

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

 

 

 

TOPICS
How to
218
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 ,
Mar 07, 2025 Mar 07, 2025

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;

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 ,
Mar 07, 2025 Mar 07, 2025

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;
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
New Here ,
Mar 07, 2025 Mar 07, 2025

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?

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 ,
Mar 07, 2025 Mar 07, 2025

Use Math.round() around the final value.

For example:

event.value = Math.round(event.value);

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
New Here ,
Mar 07, 2025 Mar 07, 2025
LATEST

Thanks! I ended up using Math.ciel() to get the rounded up value and it seems to work just as well. Thanks again!

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