Check Box and calculated Fields

Community Beginner ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

Hi Guys,

Hope you can help. I have been retired now for some time and am still using Acrobat X because I no longer need the full Creative Suite.

 

My problem is I have forgotten or am behing the times on my Javascript. This is what I have put in on a Sheet I am building for some friends.

 

if ("StrengthSavingThrow".value=="Yes")

event.value = StrMod+ProficiencyBonus;

else event.value = StrMod

This is the error code I am getting.

1:AcroForm:Str Saving Throw.0:Annot1:MouseUp:Action1

getField("StrengthSavingThrow") is null

1:Field:Calculate

3:Field:Calculate

StrMod is not defined

3:Field:Calculate

ReferenceError: StrMod is not defined

3:Field:Calculate

 

Now I have a field StrMod and The Strength Saving Throw is the checkbox. I want it if it is not checked to use one value if checked add another value to that one.

 

Any help will be appreciated.

 

Thank you very much.

 

Larry

TOPICS
PDF forms

Views

95

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

correct answers 1 Correct Answer

Adobe Community Professional , Jan 19, 2021 Jan 19, 2021
Hi, see if this works for you:var DexMod = Number(this.getField("DexMod").value); var ProBonus = Number(this.getField("ProBonus").value);if(this.getField("DexSavingThrow").value != "Off"){ event.value = DexMod + ProBonus;} else event.value = DexMod;

Likes

Translate

Translate
Adobe Community Professional ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

Is ProficiencyBonus another field in the form? If so, what type of field is it? What type of field is StrMod?

 

Regarding the script you posted, what field did you place it in and is is a calculation script?

 

I'm going to guess at the answers to those questions and suggest the following custom Calculate script:

 

// Custom Calculate script for text field
(function () {

    // Get these field values, as numbers
    var StrMod = +getField("StrMod").value;
    var ProficiencyBonus = +getField("ProficiencyBonus").value;

    // Set this field's value
    if (getField("StrengthSavingThrow").value !== "Off") {
        event.value = StrMod + ProficiencyBonus;
    } else {
        event.value = StrMod;
    }

})();

 

The first and last lines are there to prevent the unnecessary creation of document-global variables, which is a good thing. 

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 ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

Thank you for your reply. I already have the text boxes defined as numbers
with no decimals and a default of 0. With a validation script that rounds
them down.

When I plug this script in if I check the box I get no result. This is when
I want it to add the 2 numbers together. If unchecked I just want it to
retrieve the single number. Right now I have a 3 in the StrMod and a 2 in
the ProBonus. I shortened the names for efficiency in the script as well as
on the sheet.

It is like it is not looking at either field though. This time I got no
errors. So it looks like it is on the right track. Just have to figure out
how to retrieve those numbers.

Thank you again for your help.

Regards,

Larry

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 ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

Hi Mr. Johnson,
 
I have tried a few things since I got the script to ensure the fields are working correctly. 
 
1. I put in a simple calculation adding the 2 fields this gave the correct answer.
2. I tried making the default value in options the Strmod field, but this doesn't work when it is a number it has to be a number and this can change.
3. I tried changing the Off to On, but it made no difference.
4. I changed the field I am modifying so that I have one with different values. Below is the changed script though it still does not return a value. It does not act like a calculated field I can put my cursor in it and change the value.
 
// Custom Calculate script for text field (function () { // Get these field values, as numbers var DexMod = +getField("DexMod").value; var ProBonus = +getField("ProBonus").value; // Set this field's value if (getField("DexSavingThrow").value !== "Off") { event.value = DexMod + ProBonus; } else { event.value = DexMod; } })();
 
Thank you everyone for your feedback and help.

 

Regards,

Larry
 

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 ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

Have gotten it down to one error. The script reads:

 

// Custom Calculate script for text field
(function () {

// Get these field values, as numbers
var DexMod = +getField("DexMod").value;
var ProBonus = +getField("ProBonus").value;

// Set this field's value
if (getField("DexSavingThrow").value !== "Off") {
event.value = DexMod + ProBonus;
} else {
event.value = DexMod;
}

 

The error I am getting is:

 

SyntaxError: missing } after function body
13:

I have tried putting a ) in various places on line 13 but it does not seem to address it.

Regards

 

Larry

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 ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

Hi, see if this works for you:

var DexMod = Number(this.getField("DexMod").value);
var ProBonus = Number(this.getField("ProBonus").value);

if(this.getField("DexSavingThrow").value != "Off"){
event.value = DexMod + ProBonus;}
else
event.value = DexMod;

 

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 ,
Jan 20, 2021 Jan 20, 2021

Copy link to clipboard

Copied

LATEST

Thank you Nesa worked perfectly. 

 

Thank you everyone for your help.

 

Regards,

 

Larry

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