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

Make field dependent on total number of radio buttons input variable.

New Here ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

On the PDF form I have created I would like to create the following functionality.

  • If all standards (1.1.1, 1.1.2, 1.1.3) are "meeting" then standard 1.1 rating is "Meeting Standard "
  • If any standards (1.1.1, 1.1.2, 1.1.3) are "working towards" then standard 1.1 rating is "Working Towards Standard"
  • If no criteria selected then "Not Yet Rated" is displayed.

I have followed these steps to achieve this but the calculation is not performing correctly.

  1. I have set radio buttons fields "1.1.1, 1.1.2, 1.1.3" to = 1 for Meeting and = 0 for Working towards.
  2. I have created a tally "1.1 Tally" that sums these (working correctly - displays 1,2,3 as the 3 Meeting boxes are selected - counts back down if changed to working towards)
  3. I have added a calculation to field "Rating 1.1"

var Tally = this.getField("1.1 Tally").value;

if (Tally == 3) {

event.value = "Meeting Standard";

}

else if ((Tally <= 3) && (Tally >= 0)) {

event.value = "Working Towards";

}

else {

event.value = "Not Yet Rated";

}

However - I get the following results (the Tally field e.g. 3/3 is always correct)

1 x Working and 2 x Meeting = Meeting Standard (incorrect)

3 x Meeting = Working Towards (incorrect)

2 x Working 1 x Meeting = Working Towards (correct!)

Please, can someone help me understand my error? I have never used Javascript before

Thank you

TOPICS
Acrobat SDK and JavaScript

Views

686

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Did you check the calculation order?

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thank you very much! I didn't even know that existed. Working now and I simplified the formula

var v1 = this.getField("1.2 Tally").value;

if (v1 == 3) event.value = "Meeting"

else event.value = "Working Towards"

regardless of if a selection is made I get the result 0 which returns "working towards" - but I would like to show "Not Yet Rated"

is there a way that I can make it so 1.1. Tally shows blank if nothing selected for the three radio button groups and  add a condition - if Tally 1.1 is blank then show  "Not Yet Rated".

Thank you

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Use this:

if (v1 == 3) { 

event.value = "Meeting"; 

else if (v1 < 3 && v1 > 0) { 

event.value = "Working Towards"; 

else { 

event.value = "Not Yet Rated"; 

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

thanks - as the radio option for Working Towards = 0 that will not work

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

What calculate script did you use to sum checkboxes?  Since you say it always shows 0 no matter what is checked, you need to work that issue first.

If the return value of the checkboxes is not 1, that could be your problem.

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

thanks for the advice. The check boxes are 1 and when all ticked the result is now meeting which is perfect.

For the tally field I have just used the simple sum calculate option and selected the three feilds. Should a use a custom calculation that says if selection made return sum if no selection return blank? 

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Can you share the file via a google drive link?

If it shows the correct value when all three are ticked, but shows 0 otherwise, my guess is that at least one of you radio has an incorrect export value associated to the "working towards".

Also, take into consideration that if non of the two choices is made, the radio returns "Off" which cause an error in your calculation.

You might want to create a custom calculation indeed

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thanks! Here is the link: https://drive.google.com/open?id=1_sYS9EzqxZaIv_FxUKGiSsLKdZ1otqtv

In these scenarios the total is correct

WT . M

0 [ ]    1

0 [ ]    1

0 [ ]    1

Total = 3

WT . M

0 [ ]    1

0    1[ ]

0 [ ]    1

Total = 2

WT . M

0    1[ ]

0    1[ ]

0 [x ]  1[ ]

Total = 0

It is for this scenario that the total is 0 and I need it to be blank so that the form has no total and no rating when no radio buttons are selected (blank)

WT . M

0 [ ]   1[ ]

0 [ ]   1[ ]

0 [ ]   1[ ]

Total = 0

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

in that case:

//Counts how many Un-checked boxes there are

var isOff = 0

for (var x=1;x<=3;x++){

   var f = this.getField("radio"+x)

   if (f.isBoxChecked(0) == false) isOff++

}

//If all three are un-checked, shows blank

if (isOff == 3) event.value = ""

//If at least one is checked, sum only those that are checked

else{

    var sum = 0

    for (var x=1;x<=3;x++){

       var f = this.getField("radio"+x)

       if (f.isBoxChecked(0) == true){

             sum += f.value

       }

    }

   //and display the sum

   event.value = sum

}

you will also need to update your other script to take the blank value into consideration.

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

You don't have the radio buttons named radio1, radio2, and radio3 in the form. But you use the names in your script.

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

So instead of "radio"+x for the fields 1.1.1, 1.1.2, 1.1.3 I would use "1.1."x?

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

You can use "1.1." + x

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thank you! I am unsure where I should paste this - sorry I am brand new to JS. I only started last night haha

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

you paste this in the custom calculate tab of the field where you want the total to show.  As Bernd said, you need to adjust the name of your radio field in the script, or change the name of your field to what I wrote in the script.  It doesn't matter what name you use, juste make sure you use the same (it is case sensitive) and the important part is that it ends with numbers 1 to 3 so you can loop throught them.

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thanks!

It is correctly blank when nothing is selected

It is correctly 0 when all working towards is selected

if all Meeting is selected it is incorrectly showing blank

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

What code do you use now?

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Really appreciate your help guys - I am using the code below - document is being saved here: https://drive.google.com/file/d/1_sYS9EzqxZaIv_FxUKGiSsLKdZ1otqtv/view?usp=sharing

//Counts how many Un-checked boxes there are

var isOff = 0

for (var x=1;x<=3;x++){

var f = this.getField("1.1."+x)

if (f.isBoxChecked(0) == false) isOff++

}

//If all three are un-checked, shows blank

if (isOff == 3) event.value = ""

//If at least one is checked, sum only those that are checked

else{

var sum = 0

for (var x=1;x<=3;x++){

var f = this.getField("1.1."+x)

if (f.isBoxChecked(0) == true){

sum += f.value

}

}

//and display the sum

event.value = sum

}

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Use this:

var sum = 0;

    for (var x=1;x<=3;x++){

       var f = this.getField("1.1."+x);

       sum += f.value;

    }

   if (sum == 0) event.value = "";

   else event.value = sum;

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

What happens if at least one line is empty?  Won't it concatenate an "Off" string?

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

This are radio boxes with the value 0 or 1.

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
Engaged ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

I know, but look at the outcomes describred earlier

[  ]  

[  ]  

[  ]  

sum = 3

[  ]  

   [  ]

   [  ]

sum = 1

But what about:

   [  ]

[  ]   [  ]

[  ]  

sum = "0Off1"  ???

Unless radios are required fields, that poses a problem with your solution

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 Expert ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

The third sample is not possible in the form.

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

Thanks Bernd and MatLac -

I am getting an Off result for some scenarios.

Should be " "

Should be "Working Towards"

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
New Here ,
May 11, 2018 May 11, 2018

Copy link to clipboard

Copied

OK the form is working well now and correct values and showing unless one or more boxes are not checked. In which case, it appears to be concatenating the value.

e.g.

0OffOff1 - should = 1

0OffOffOff - should = " "

000Off - should = 0

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