Skip to main content
Participant
November 9, 2018
Question

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 b

  • November 9, 2018
  • 2 replies
  • 514 views

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

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
November 9, 2018

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.

Thom Parker
Community Expert
Community Expert
November 9, 2018

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
November 9, 2018

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.

Thom Parker
Community Expert
Community Expert
November 9, 2018

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 PDFScriptingUse the Acrobat JavaScript Reference early and often