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

Script to perform simple math by controlling number of variables with checkboxes

New Here ,
Oct 15, 2021 Oct 15, 2021

I've been working on a document for a few weeks with little success in performing very simple integer math.  My PDF uses 13 drop down boxes with choices that associate with different export values.  I want to output the sum of these values to a text box, but I have a unique checkbox for controlling whether each dropdown counts towards the total automatically as the boxes are checked or unchecked.  I have failed to figure this out with tutorials, examples, or reviewing other javascript questions posted in relation to this.  Most likely I am further from a correct solution than when I started at this point.  My current attempt is linked below, but excluding several of the lines as all 13 sets are essentially identical minus naming convention variations.  Any insight is greatly appreciated.  (Fields with B# are the checkboxes, and R# are the dropdown lists):

 

var math = 0;
var RCSV1 = 0;
var RCSV9 = 0;
var ECSV1 = 0;
var ECSV3 = 0;
var FCSV1 = 0;

 

if (getField("RequiredCourseSelectB1").value!="Off") RCSV1.value = this.getField("RequiredCourseSelectR1").exportValues
else RCSV1.value = 0;

if (getField("RequiredCourseSelectB9").value!="Off") RCSV9.value = this.getField("RequiredCourseSelectR9").exportValues
else RCSV9.value = 0;

if (getField("RestrictedCourseSelectB1").value!="Off") ECSV1.value = this.getField("RestrictedCourseSelectR1").exportValues
else ECSV1.value = 0;

if (getField("RestrictedCourseSelectB2").value!="Off") ECSV2.value = this.getField("RestrictedCourseSelectR2").exportValues
else ECSV3.value = 0;

if (getField("FreeCourseSelectB1").value!="Off") FCSV1.value = this.getField("FreeCourseSelectR1").exportValues
else FCSV1.value = 0;

 

math.value = RCSV1.value + RCSV2.value + RCSV3.value + RCSV4.value + RCSV5.value + RCSV6.value + RCSV7.value + RCSV8.value + RCSV9.value + ECSV1.value + ECSV2.value + ECSV3.value + FCSV1.value;

 

this.getField("TotalCreditsCalc").value=math.value;

TOPICS
JavaScript , PDF forms
610
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 ,
Oct 16, 2021 Oct 16, 2021

In dropdown field -> properties -> options tab tick 'Commit selected value immediately.

Don't use 'exportValues' use only 'value' and don't set variables with suffix 'value', use just variable name.

Here is short example how it should look, adapt your code to that:

var math = 0;
var RCSV1 = 0;
var RCSV9 = 0;

if(this.getField("RequiredCourseSelectB1").valueAsString != "Off")
RCSV1 = Number(this.getField("RequiredCourseSelectR1").value);
if(this.getField("RequiredCourseSelectB9").valueAsString != "Off")
RCSV9 = Number(this.getField("RequiredCourseSelectR9").value);
math = RCSV1+RCSV9;
this.getField("TotalCreditsCalc").value = math;

 

If you use code in "TotalCreditsCalc" field change last line to:

event.value = math;

If you need additional help let me know.

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 ,
Oct 16, 2021 Oct 16, 2021

In dropdown field -> properties -> options tab tick 'Commit selected value immediately.

Don't use 'exportValues' use only 'value' and don't set variables with suffix 'value', use just variable name.

Here is short example how it should look, adapt your code to that:

var math = 0;
var RCSV1 = 0;
var RCSV9 = 0;

if(this.getField("RequiredCourseSelectB1").valueAsString != "Off")
RCSV1 = Number(this.getField("RequiredCourseSelectR1").value);
if(this.getField("RequiredCourseSelectB9").valueAsString != "Off")
RCSV9 = Number(this.getField("RequiredCourseSelectR9").value);
math = RCSV1+RCSV9;
this.getField("TotalCreditsCalc").value = math;

 

If you use code in "TotalCreditsCalc" field change last line to:

event.value = math;

If you need additional help let me know.

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 ,
Oct 18, 2021 Oct 18, 2021
LATEST

I appreciate this so much Nesa!  With some minor tweaks, I've got my document in functional condition finally.  Now I just need to sort out how to embed a button to export only some of the fields into an updating excel spreadsheet, but that's a tribulation for another day.  I'm sure you'll hear from me when I fail with that too.  Thanks for your time!

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