Show a field if all checkboxes are checked

Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

I have a PDF checklist with 12 checkboxes named Pass1.0, Pass1.1 and so on up to Pass1.11.

There's a Visible text field called CompletePrompt at the top of the page on top of a Hidden signature field.

There is a Sign Checklist button at the bottom of the page. When clicked, the following js runs:

this.getField("CompletePrompt").display = display.hidden;

this.getField("ReviewerSignature").display = display.visible;

this.getField("ReviewerSignature").setFocus();

this.getField("ReviewerSignature").strokeColor = color.red;

I want this to happen if and only if every one of the 12 checkboxes is checked. If even one box is blank, I want the script to end and trigger an app.alert without toggling the visibility of the other fields.

My current effort ended in:

var f = this.getField("Pass1");

if (f.valueAsString == "Off") {

     app.alert({cMsg:"Check all Checkboxes to Sign"});

} else if (f.valueAsString == "Yes") {

     this.getField("CompletePrompt").display = display.hidden;

     this.getField("ReviewerSignature").display = display.visible;

     this.getField("ReviewerSignature").setFocus();

     this.getField("ReviewerSignature").strokeColor = color.red;

}

What am I doing wrong here?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

305

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

Show a field if all checkboxes are checked

Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

I have a PDF checklist with 12 checkboxes named Pass1.0, Pass1.1 and so on up to Pass1.11.

There's a Visible text field called CompletePrompt at the top of the page on top of a Hidden signature field.

There is a Sign Checklist button at the bottom of the page. When clicked, the following js runs:

this.getField("CompletePrompt").display = display.hidden;

this.getField("ReviewerSignature").display = display.visible;

this.getField("ReviewerSignature").setFocus();

this.getField("ReviewerSignature").strokeColor = color.red;

I want this to happen if and only if every one of the 12 checkboxes is checked. If even one box is blank, I want the script to end and trigger an app.alert without toggling the visibility of the other fields.

My current effort ended in:

var f = this.getField("Pass1");

if (f.valueAsString == "Off") {

     app.alert({cMsg:"Check all Checkboxes to Sign"});

} else if (f.valueAsString == "Yes") {

     this.getField("CompletePrompt").display = display.hidden;

     this.getField("ReviewerSignature").display = display.visible;

     this.getField("ReviewerSignature").setFocus();

     this.getField("ReviewerSignature").strokeColor = color.red;

}

What am I doing wrong here?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

306

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
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

The value property is unique to each field. You can't access all of the fields' values by access the parent one.

You'll need to use a loop to iterate over all the fields, checking all of their values.

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...
Jul 26, 2018 1
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

Edit: I tried the below code. The error message works perfectly. However, when I check all the checkboxes and then click the Sign Checklist button, an error message is displayed in the JS debugger "Type f is null".

I'm not the greatest at js loops, i know I have to use something like the following:

for (var i=0; i<=12; i++) {

     var f = this.getField("Pass1."+i);

          if (f.valueAsString=="Off"){

               app.alert({cMsg:"Check all Checkboxes to Sign"});

               break;

          } else if (f.valueAsString=="Yes"){

               this.getField("CompletePrompt").display = display.hidden;

               this.getField("ReviewerSignature").display = display.visible;

               this.getField("ReviewerSignature").setFocus();

               this.getField("ReviewerSignature").strokeColor = color.red;

          }

     }

Could you please let me know if I have the syntax wrong? I'm basing it off one of your answers on these forums.

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...
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

The syntax is fine, but the logic you used is incorrect. You should use a Boolean variable to keep track of the values of the fields inside the loop (if the value is "Off" set it to false, otherwise leave it as true), and after it check that variable to make sure that none of the fields were off (ie, if the variable is true, it's all good). Then show the error message/enable the fields based on 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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

Similar in a way to what you did here (re: the variable)?

Source: Re: go through several checkboxes if all checked than check one checkbox

var allChecked = true; 

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

    var f = this.getField("cb"+i); 

    if (f.valueAsString=="Off") { 

        allChecked = false; 

        break; 

    } 

this.getField("cb0").checkThisBox(0, allChecked);

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...
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

Exactly...

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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

Hi. I'm sorry for bugging you, but:

var allChecked = true;

  

for (var i=0; i<=12; i++) {

  var f = this.getField("Pass1."+i);

    if (f.valueAsString=="Off"){

      allChecked = false;

      break;

}

if (allChecked.value == "true") {

    this.getField("CompletePrompt").display = display.hidden;

    this.getField("ReviewerSignature").display = display.visible;

    this.getField("ReviewerSignature").setFocus();

    this.getField("ReviewerSignature").strokeColor = color.red;

} else {

    app.alert({cMsg:"Complete checklist first!"});

}

}

Nothing happens now. I'm pretty sure I messed up the syntax somewhere, I just don't know what.

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...
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

You need to carefully look at which pair of curly brackets match, and what

that means for the code in 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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

Christ, it's been a long day at work, I think. Thanks so much for the help - really appreciate. Just for reference, this is the final code that ended up working properly (im sure it could be trimmed but yes):

var allChecked = true;

   

for (var i=0; i<=12; i++) { 

  var f = this.getField("Pass1."+i); 

     if (f.valueAsString=="Off"){ 

       allChecked = false;

       break;

} else if (f.valueAsString=="Yes"){

               allChecked = true;

               break; }

}

if (allChecked == false) {

    app.alert({cMsg:"Complete checklist first!"});

} else if (allChecked == true){

    this.getField("CompletePrompt").display = display.hidden; 

    this.getField("ReviewerSignature").display = display.visible; 

    this.getField("ReviewerSignature").setFocus(); 

    this.getField("ReviewerSignature").strokeColor = color.red;

}

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...
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

You still have an error in it that will prevent it from working correctly. The entire else-if block in the for-loop is not necessary (lines 8-10).

You should remove 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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

Hi, so I was testing and found out that for some reason, the code is broken for the first checkbox, Pass1.0. If that one is unchecked, the alert works, and if that one is checked, the other conditions work.

It appears that the script is only checking that first checkbox. I tried renaming each checkbox to Pass1, Pass2, so on until 12 and then adjusting the code to ("Pass"+i) in the for statement as such:

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

  var f = this.getField("Pass"+i);

    if (f.valueAsString=="Off"){

      allChecked = false;

      break;

} else if (f.valueAsString=="Yes"){

              allChecked = true;

              break; }

}

However, the same problem appears.

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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

You're a genius, sir. 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...
Jul 26, 2018 0
Most Valuable Participant ,
Jul 26, 2018

Copy link to clipboard

Copied

So my previous reply...

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...
Jul 26, 2018 0
Community Beginner ,
Jul 26, 2018

Copy link to clipboard

Copied

This is the final code snippet that works to hide a Signature field under a different text field until all checkboxes in a list are checked off.

var allChecked = true;

  

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

  var f = this.getField("Pass"+i);

    if (f.valueAsString=="Off"){

      allChecked = false;

      break;

}

}

if (allChecked == false) {

    app.alert({cMsg:"Complete checklist first!"});

} else if (allChecked == true){

    this.getField("CompletePrompt").display = display.hidden;

    this.getField("ReviewerSignature").display = display.visible;

    this.getField("ReviewerSignature").setFocus();

    this.getField("ReviewerSignature").strokeColor = color.red;

}

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...
Jul 26, 2018 0