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

New Here ,
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, Macintosh

Views

491

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

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

New Here ,
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, Macintosh

Views

492

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
May 10, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 1
New Here ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
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
Reply
Loading...
May 11, 2018 0
Adobe Community Professional ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
New Here ,
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
Reply
Loading...
May 11, 2018 0
Engaged ,
May 11, 2018

Copy link to clipboard

Copied

Of course you are.  In JS, like in every language, data has a type.  It can be a number, a string, an array, a boolean, etc.  JS is a dynamic language, as opposed to a static language.  In a static language, like JAVA for instance, you need to declare variables, but you also need to declare the type or it will not work.  JS, being dynamic, will "presume" the type, and will also dynamicaly change type when needed, without the need for the coder to do it specifically.  This speeds up the coding process but will sometimes induces errors when the coder is not aware of what is happenig.  You can return the type of the data with the typeof command like so:

var myData = 9

app.alert(typeof myData) //will output "number" as an alert

var myData = []

app.alert(typeof myData) //will output "array" as an alert

In JS, the "+" operator is an addition operator when working with numbers, but it becomes a concatenation operator when working with strings (meaning it will append strings one after the other).  But what happens when you try add a number (0) to a string ("Off")?  As soon as it its a string, it converts any number it encountered to a string and concatenate it.

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
Reply
Loading...
May 11, 2018 0
Engaged ,
May 11, 2018

Copy link to clipboard

Copied

this is why this part:

//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

       }

    }

is important.  It will sum up only boxes that are checked (that return a number) and will skip the ones returning a string ("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
Reply
Loading...
May 11, 2018 0
New Here ,
May 11, 2018

Copy link to clipboard

Copied

Thanks I appreciate the explanation.

I have tried your original formula MatLac and it is not summing the Meeting boxes. It returns 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
Reply
Loading...
May 11, 2018 0
Engaged ,
May 11, 2018

Copy link to clipboard

Copied

I tested it myself.  There is a problem with it so try this instead:

//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.value == "Off") isOff++

}

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

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

//If at least one is checked, sum only those that have a value of 1

else{

    var sum = 0

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

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

       if (f.value == 1)  sum++

    }

   //and display the sum

   event.value = sum

}

For some reason, isBoxChecked(0) only returned true when the first check box was checked, not the second one.......So I tested its value instead.  For the sum part, I tested if the value is equal to 1 (because it is the only case where the value will be incremented) and just used the increment operator "++"

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
Reply
Loading...
May 11, 2018 1
New Here ,
May 11, 2018

Copy link to clipboard

Copied

Thanks MatLac!

This worked.

Would you please mind checking this script for me. Now that I have Rating 1.1 1.2 and 1.2 correctly populating I would like to total the number of ratings that are "meeting"

I have tried to first assign each variable a value of 1 only if the field value is Meeting. Then I would like to sum these together so I get a number /3

if (this.getField("Rating 1.1").value =="Meeting") {

    var v1 = 1

} else {

    var v1 = 0

}

if (this.getField("Rating 1.2").value =="Meeting") {

    var v2 = 1

} else {

    var v2 = 0

}

if (this.getField("Rating 1.3").value =="Meeting") {

    var v3 = 1

} else {

    var v3 = 0

}

var sum = v1 + v2 + v3

event.value = sum

If all are "Meeting" I correctly get a value of 3 however - if two of the rating fields are Working Towards and the other Meeting, I get a value of 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
Reply
Loading...
May 11, 2018 0
MatLac LATEST
Engaged ,
May 14, 2018

Copy link to clipboard

Copied

This seems ok altought it is a bit long.

You could really shorten it by assigning a 0 value from the sart to all three variables and increment it to 1 only if needed like so:

var v1 = 0

var v2 = 0

var v3 = 0

if (this.getField("Rating 1.1").value =="Meeting") v1++

if (this.getField("Rating 1.2").value =="Meeting") v2++

if (this.getField("Rating 1.3").value =="Meeting") v3++

var sum = v1 + v2 + v3

event.value = sum

The middle block looks repetitive, don't it? How about using a loop and an array instead:

var aValues = [0,0,0]

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

}

var sum = 0

for (i in aValues) sum += aValues

event.value = sum

Well, look at that, we iterated twice in the same array.  Why not do both operations in one loop?

var aValues = [0,0,0]

var sum = 0

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

    sum += aValues

}

event.value = sum

We can even get rid of sum and operate directly with on the field value

var aValues = [0,0,0]

event.value = 0

for (var i in aValues){

    if (this.getField("Rating 1."+(Number(i)+1)).value =="Meeting") aValues++

    event.value += aValues

}

Do we need an array at all?  Definitly not. Lets iterate 3 times instead

event.value = 0

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

if (this.getField("Rating 1."+i).value =="Meeting") event.value++

}

Do we even need brackets? nope

event.value = 0

for (var i=1;i<=3;i++) if (this.getField("Rating 1."+i).value =="Meeting") event.value++

So that 17 lines code you wrote can be achieved as a 2 lines code.  Whenever you feel something is repetitive and long, it's because you are doing it wrong.  It takes experience but it will come.

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
Reply
Loading...
May 14, 2018 0