Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Trying to use Checkboxes for conditional math

Community Beginner ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

Hello all,

 

I'm creating a character sheet, and here's the rub:

I have a checkbox: Tag01

I have a Text field: Skill01

The text field can be edited by the user.

If the check box is checked, the Text field increments by 2.

 

The part I can't figure out is: when the check box is unchecked, that +2 bonus is removed, BUT I don't want it to continually subtract 2 from any values placed as it starts unchecked by default.

Pseudocode:

If Tag01 becomes checked: +2 to Skill01 value.

When Tag01 becomes unchecked: subtract +2 bonus to Skill01 value, but only if bonus was applied to begin with.

 

I hope that makes sense.

TOPICS
Create PDFs, JavaScript, PDF forms

Views

418

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

So you want to have a field that is both editable and calculates at the same time. These two things are logically incompatible.  Anytime the calculation executes it use the current value in the calculation. 

You can't actually have a field calc act on itself and not have wild, and possibly fatal results. But, what you can do is create the appearance of the field acting the way you want, by putting the entered value into a hidden field, then using the hidden field in the calculation. 

This is tricky business.

First, create a hidden field named "HiddenValue".

Now use the committed keystrok script to save the entered value. Enter this script into the custom keystroke script for the "Skill01" field. 

 

if(event.willCommit) this.getField("HiddenValue").value = event.value;

Now use this script in the Custom Calcualtion for the "Skill01" field

 

var nVal = this.getField("HiddenValue").value;
if(this.getField("Tag01").value != "Off")
   nVal +=2;
event.value = nVal;

  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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 ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Wow! That's a great answer! I didn't think of it that way.

 

I'll try this out immediately!

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

How is the script supposed to know if the bonus was applied or not to begin with?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

What I would do is set the check-box to add 2 when ticked, and subtract 2 when un-ticked, but also set the text field to reset the check-box when its value is edited... I think that will fulfill your requirements.

Likes

Translate

Translate

Report

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 ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

How would I do that? I can make the field increment on check, but not subtract or reset.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

You can use this code as the Mouse Up event of the check-box:

 

var f = this.getField("Skill01");
f.value += (event.target.value=="Off") ? -2 : 2;

 

And this as the custom validation of the text field:

this.getField("Tag01").value = "Off";

Likes

Translate

Translate

Report

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 ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Thanks!

Likes

Translate

Translate

Report

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
Enthusiast ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

It's on the right track but I belive you need more complex string,for example what if you check checkbox and then change value in field? it won't add 2 to new value and if you uncheck it will remove 2 from new value.

Likes

Translate

Translate

Report

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 ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

LATEST

Actually, that's perfectly fine. Thank you for your help.

Likes

Translate

Translate

Report

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