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

I want to have a checkbox if based on the values from a combination of other checkboxes, ex; if 2 or more of 7 checkboxes are filled I want checkbox 8 to autofill. if only 1 of other 7 checkboxes are filled checkbox 8 does not autofill and is left blank

New Here ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

I want to have a checkbox if based on the values from a combination of other checkboxes, ex; if 2 or more of 7 checkboxes are filled I want checkbox 8 to autofill.  if only 1 of other 7 checkboxes are filled checkbox 8 does not autofill and is left blank

TOPICS
Acrobat SDK and JavaScript

Views

273

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 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

So you need to count the checked boxes. The best way to do this is to use a group naming convention for the first 7 checks, like this: "MyGroup.check1", "MyGroup.check2",  etc.

Then this code will return true/false based on your criteria

   var nCnt = 0;

   this.getField("MyGroup").getArray().foreach(function(a){if(a.value == "Yes") nCnt++;});

    this.getField("Check8").value = (nCnt>1)?"Yes":"Off";

Place this code in the MouseUp event of each of the 7 checkboxes.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

I would do it using a (hidden) text field with the following calculation script (let's say the fields are named Check1 to Check8):

var ticked = 0;

for (var i=1; i<=7; i++) {
    if (this.getField("Check"+i).valueAsString!="Off") ticked++;
}

this.getField("Check8").tickThisBox(0, (ticked>=2));

The main benefit of doing it like that is that it will also work when the form is cleared, while the MouseUp approach won't.

Also, you have a single location with all the code necessary and you don't need to apply the same function to multiple fields.

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 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

True, but it also lowers performance because it runs whenever any field is changed, not just when it is needed. I try to avoid unnecessary calculation whenever possible.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

LATEST

That's true as well, although that's not a big deal if there aren't many calculated fields in the file.

Also, there are ways around it (by checking the event's source), while still allowing it to reset when the whole form is cleared.

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