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

Conditional Formatting for multiple checkboxes, into one

Explorer ,
Nov 24, 2020 Nov 24, 2020

Copy link to clipboard

Copied

Hello all,

 

So if necessary, since I know I can get a little long winded I put a tl:dr at the end. Thanks for your patience if you read all the way through!!

 

I am working on modifying/improving a pdf form utilizing checkboxes that would communicate to other checkboxes, where the other checkboxes provide a total score. This is for conducting an evaluation on a given process.... What I'm trying to do is get a script so that if there are 5 requirements that need to be met just to receive the check that indicates pass instead of fail (multiple pass/fail events across the board). The trick is, if just any one of the 5 requirements is missed the check is not received, indicating that the entire event is failed. I'm hoping this is a clear enough scenario for someone to provide a base script that I can integrate, and maybe even build off of. I can read java, but have a hard time building without a base. Any help is greatly appreciated, and thanks in advance!!

 

tl:dr

trying to obatin a base script for making five checked checkboxes cause a sixth to check, with one stipulation; if any one of the five checkboxes are not checked, the sixth checbox will not get checked. Sidenote: can read and build off javascript, but novice enough where I can't create without a base script.

TOPICS
Acrobat SDK and JavaScript

Views

2.5K

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

Community Expert , Nov 25, 2020 Nov 25, 2020

Then use this code and put in in every checkbox 1-8

var c1 = this.getField("Check 1").valueAsString;
var c2 = this.getField("Check 2").valueAsString;
var c3 = this.getField("Check 3").valueAsString;
var c4 = this.getField("Check 4").valueAsString;
var c5 = this.getField("Check 5").valueAsString;
var c6 = this.getField("Check 6").valueAsString;
var c7 = this.getField("Check 7").valueAsString;
var c8 = this.getField("Check 8").valueAsString;
var c9 = this.getField("Check Box3");
if(c1 != "Off" && c
...

Votes

Translate

Translate
Community Expert ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

One option is to make a hidden text field to use code in it as "Custom Calculation Script".
Lets say your checkboxes are named Check Box1 to Check Box6,
you can use code like this in hidden text field:

var check = "";
for (var i=1; i<=5; i++) {
if (this.getField("Check Box"+i).valueAsString != "Off")check++}
this.getField("Check Box6").value = check == 5 ? "Yes" : "Off";

Where "Yes" is an export value of "Check Box6" in case you change it to something else.

 

Alternative to above is to use code in all 5 checkboxes as Mouse Up event like this:

var c1 = this.getField("Check Box1").valueAsString;
var c2 = this.getField("Check Box2").valueAsString;
var c3 = this.getField("Check Box3").valueAsString;
var c4 = this.getField("Check Box4").valueAsString;
var c5 = this.getField("Check Box5").valueAsString;
var c6 = this.getField("Check Box6");
if(c1 != "Off" && c2 != "Off" && c3 != "Off" && c4 != "Off" && c5 != "Off"){
c6.value = "Yes";}
else c6.value = "Off";

Rename fields if necessary.

 

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

++Adding to the discussion,

 

Or you can also add this line to every checkbox except for "Check Box6":

 

var ck = this.getField("Check Box6");

(event.target.value !== "Off") ?   ck.checkThisBox(0, true) :  ck.checkThisBox(0, false);

 

 

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

That would check "Check Box6" when any check box is checked.

From my understanding OP want to check "Check Box6" only if all 5 are checked and not before.

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Well, you can still apply your original code without a hidden field and run it on each of the  radio buttons except for checkbox 6. 

 

The only twist to the ideas already suggested above is  two conditional statements instead of having the script check the On and Off status for each checkbox widget.

 

This is how I combined our ideas into one single script and it works very good:

 

var check = 0;

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

if (this.getField("Check Box"+i).valueAsString != "Off") {

check++

if (check ==5)  this.getField("Check Box6").checkThisBox(0, true) 

if (check <= 4) this.getField("Check Box6").checkThisBox(0, false);
 }
}

 

I am sure there are other even  easier ways (or more elegant scripts) to accomplish the same, but I liked how you thought of the for loop idea and it works without having to place it in a hidden field. 

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

When I took this code, based on my just published update, the code looked like this:

var c1 = this.getField("Check 1").valueAsString;
var c2 = this.getField("Check 2").valueAsString;
var c3 = this.getField("Check 3").valueAsString;
var c4 = this.getField("Check 4").valueAsString;
var c5 = this.getField("Check 5").valueAsString;
var c6 = this.getField("Check 6").valueAsString;
var c7 = this.getField("Check 7").valueAsString;
var c8 = this.getField("Check 8").valueAsString;
var c9 = this.getField("Check Box3");
if(c1 != "Off" && c2 != "Off" && c3 != "Off" && c4 != "Off" && c5 != "Off" && c6 != "Off" && c7 != "Off" && c8 != "Off"){
c9.value = "Yes";}
else c9.value = "Off";

However, it does not check c9..

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Wrong person, sorry!

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Make sure your checkboxes are named correctly.

Is export value for "Check Box3" Yes?

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

The export value for "Check Box3" is actually 1. When I changed it to that, there was no change...I will test it again, just to be sure.

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Yeah, no change.

 

The reason for it being 1, is so that "Check BoxZ" (z equalling 1 through 38 vice listing them all) can provide it's respective points to a total score.

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Can you share file?

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Unfortunately no... however, I did some extra digging and came up with the below code that for some reason likes to kind of work. The only thing it does not do is automatically check/uncheck "Check Box3".

if(this.getField("Check 1").value == "Yes" && this.getField("Check 2").value == "Yes"  && this.getField("Check 3").value == "Yes" && this.getField("Check 4").value == "Yes" && this.getField("Check 5").value == "Yes" && this.getField("Check 6").value == "Yes" && this.getField("Check 7").value == "Yes" && this.getField("Check 8").value == "Yes") {
this.getField("Check Box3").checkThisBox(0, true);
}

 

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

And what does that code do?

Did you put codes in every checkbox 1-8?

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

What it does is when Check 1 - 8 are checked I still have to manually check Check Box3, but it will not allow me to uncheck Check Box3 until any one of Check 1 - 8 becomes un-checked. Basically with the double ampersands it requires each individual condition to be met, where the condition is pulling directly from the field vice a variable.

No, only Check Box3; as I figure the condition is only required by Check Box3.

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

That won't work. Do you want to manually check Check Box3 or do you want it auto checked once check boxes 1-8 are checked?

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

I want it to automatically check Check Box3, only if Check 1-8 are checked.

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Then use this code and put in in every checkbox 1-8

var c1 = this.getField("Check 1").valueAsString;
var c2 = this.getField("Check 2").valueAsString;
var c3 = this.getField("Check 3").valueAsString;
var c4 = this.getField("Check 4").valueAsString;
var c5 = this.getField("Check 5").valueAsString;
var c6 = this.getField("Check 6").valueAsString;
var c7 = this.getField("Check 7").valueAsString;
var c8 = this.getField("Check 8").valueAsString;
var c9 = this.getField("Check Box3");
if(c1 != "Off" && c2 != "Off" && c3 != "Off" && c4 != "Off" && c5 != "Off" && c6 != "Off" && c7 != "Off" && c8 != "Off"){
c9.value = "Yes";}
else c9.value = "Off";

Votes

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 ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

If you don't want to put codes in every checkbox then create hidden text field and put this code in Custom calculation script.

var check = "";
for (var i=1; i<=8; i++) {
if (this.getField("Check "+i).valueAsString != "Off")check++}
this.getField("Check Box3").value = check == 8 ? "Yes" : "Off";

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

LATEST

Okay, I see how it works now!! Thank you very much!!!!!!!!

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

When I took this code, based on my just published update, the code looked like this:

var c1 = this.getField("Check 1").valueAsString;
var c2 = this.getField("Check 2").valueAsString;
var c3 = this.getField("Check 3").valueAsString;
var c4 = this.getField("Check 4").valueAsString;
var c5 = this.getField("Check 5").valueAsString;
var c6 = this.getField("Check 6").valueAsString;
var c7 = this.getField("Check 7").valueAsString;
var c8 = this.getField("Check 8").valueAsString;
var c9 = this.getField("Check Box3");
if(c1 != "Off" && c2 != "Off" && c3 != "Off" && c4 != "Off" && c5 != "Off" && c6 != "Off" && c7 != "Off" && c8 != "Off"){
c9.value = "Yes";}
else c9.value = "Off";

However, when c1-c8 are checked it does not check c9...

Votes

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
Explorer ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

Update: the amount of checkboxes that trigger the one to become checked is 8

Votes

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