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

Checkbox to select and deselect other checkboxes

Community Beginner ,
Feb 21, 2018 Feb 21, 2018

I am working on an Acrobat form which as four checkboxes. What I am trying to achieve are two things:

1. If any checkbox is selected, it selects all previous checkboxes.

2. If any checkbox is deselected, it deselects all following checkboxes, leaving the current and previous checkboxes selected.

Any assistance would be appreciated.

TOPICS
PDF forms
11.9K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Feb 26, 2018 Feb 26, 2018

Don't use that script, it makes no sense.

Here is the MouseUp script for Checkbox 18, all the others are variations on this,

if(event.target.isBoxChecked(0))

{// Perform actions when checked

   this.getField("Check Box17").checkThisBox(0,true);

}

else

{// Perform actions when unchecked

   this.getField("Check Box19").checkThisBox(0,false);

   this.getField("Check Box20").checkThisBox(0,false);

}

When you write out the description of what a checkbox will do you must include all actions, both checked and unchecked, all in one place.

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

View solution in original post

Translate
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 ,
Feb 21, 2018 Feb 21, 2018

This has been asked many times. Here's some search results from this forum:

https://forums.adobe.com/search.jspa?q=check+all

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

Translate
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 Beginner ,
Feb 21, 2018 Feb 21, 2018

Thom,

I wasn't clear when I mentioned ALL previous and following checkboxes on the form, as I need to apply the check/uncheck to only four checkboxes out of a total of 83. They are labelled Check Box17, Check Box 18, Check Box19 and Check Box20.

So I need the following to happen:

If Check Box20 is selected, it also selects Check Boxes 17, 18 and 19

If Check Box19 is selected, it also selects Check Boxes 17 and 18

If Check Box18 is selected, it also selects Check Box17

In addition to the above, I need it to do this:

If Check Box17 is deselected, it also deselects Check Boxes18, 19 and 20

If Check Box18 is deselected, it also deselects Check Boxes19 and 20 (leaving Check Box17 selected)

If Check Box19 is deselected, it also deselects Check Box20 (leaving Check Box17 and 18 selected)

Thank you

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

The solutions provided in other forum threads on checking and unchecking all follow the same pattern, even though many of them are for very different purposes.  You're solution is no different.

1. Use the MouseUP script on each checkbox that does something.

2. Test the value of the current checkbox with event.value

3. If the checkbox is turned on, then modify some other checkboxes

4. If  the checkbox is turned off, then modify some other checkboxes in a different way.

You'll find all the code you need in the other threads. 

One other note. In the last two instructions for how the check boxes should work, and really for all of the instructions, the text in the parentheses should read (Leaving CheckBox X, etc. unchanged) because the value of the unaffected checkboxes is unknown, because they are unaffected by the script. This is an important point when designing logic. You have to cover all the potential variations, without making assumptions.

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

Translate
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 Beginner ,
Feb 25, 2018 Feb 25, 2018

Thom

I found this script which I used in the mouse up for "Check Box18" which also selects "Check Box17" when checked.

I have also used this script for "Check Box19" (adding "Check Box18" to the var fn), and for "Check Box20" (adding "Check Box 18", "Check Box19" to the var fn). These are working fine for selecting the previous checkboxes, but I am having trouble incorporating a script which deselects Check Boxes 19 and 20 when "Check Box18" is deselected (same for "Check Box19" which deselects "Check Box20").

As a graphic designer scripting is a foreign language to me. The only script I am familiar with, is a font style!

this.calculate = false;

var fn = ["Check Box17"];

for (var i = 0; i < fn.length; i++) {

    if (this.getField(fn).type == "checkbox") {

        this.getField(fn).checkThisBox(0, true) ;

    }

}

this.calculate = true;

this.calculateNow();

Translate
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 ,
Feb 26, 2018 Feb 26, 2018

Don't use that script, it makes no sense.

Here is the MouseUp script for Checkbox 18, all the others are variations on this,

if(event.target.isBoxChecked(0))

{// Perform actions when checked

   this.getField("Check Box17").checkThisBox(0,true);

}

else

{// Perform actions when unchecked

   this.getField("Check Box19").checkThisBox(0,false);

   this.getField("Check Box20").checkThisBox(0,false);

}

When you write out the description of what a checkbox will do you must include all actions, both checked and unchecked, all in one place.

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

Translate
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 Beginner ,
Feb 26, 2018 Feb 26, 2018

Thank you Thom

I have applied the above script adding "Group1." as a prefix, as all my checkboxes have this prefix. The checkboxes are working as required, however another field which lists all the export values from the checked items are now in a random order (not in checkbox number order). This is the script for that field. Is there something I can add to it, to force the export values from the checked items to appear in checkbox number order?

var oField; // variable for field;

var cValue; // variable for field value;

var aAwards = new Array(); // array for check box values not "Off";

var aFlds = this.getField("Group1").getArray();

for(var i = 0; i < aFlds.length; i++)

{

     cValue = aFlds.value;

     if(cValue != "Off")

     {

          aAwards.push(cValue);

     } // end field value not "Off";

} // end i loop;

event.value = aAwards.join("\r");

Translate
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 ,
Feb 26, 2018 Feb 26, 2018

There is no specification for the order in which the fields appear in the list.  However, its possible it follows the tabbing order, try it out. If this doesn't work then you can sort the fields array, if there is something to sort on. Does the order follow the the number at the end of the field name?

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

Translate
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 Beginner ,
Feb 27, 2018 Feb 27, 2018

HI Thom

I have sorted the tabbing order, however the items in the field are still out of order. All the checkboxes have a number at the end ("Group1.Check Box1" to "Group1.Check Box83"), but some of them have the same name with different export values. The reason for this is that some sections of the form with checkboxes need to be selected as one only. For example, two checkboxes are labelled "Group1.Check Box28" and appear in the field listing at the right of the screen as "Group1.Check Box28#0" and "Group1.Check Box28#1". Could this be affecting the order in which the checkbox values appear in the field list?

Translate
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 ,
Feb 27, 2018 Feb 27, 2018

If two or more fields have the same name, then they are essentially the same field. The separate instances are called widgets of the field. It's possible that the order of the widgets is causing an issue.  However, if the field order matches the numbers then you can easily sort them.  I would suggest making sure they all have the same number of digits on the end, so "Group1.Check Box1" becomes "Group1.Check Box01".  Then use this code when getting the array

var aFlds = this.getField("Group1").getArray().sort();

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

Translate
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 Beginner ,
Feb 28, 2018 Feb 28, 2018

Hi Thom

I have applied your suggestion above, however the items in the field are still out of order. It appears that the widgets may be causing the problem, as these are the ones that are out of order in the field (they appear in the list at the end of the others).

Translate
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 ,
Mar 01, 2018 Mar 01, 2018

As I stated previously, fields with the same name are the same field. The export value for one widget is identical as the next widget with the same name. So, the widget variations do not appear the list return by the "getArray()" function. They cannot be out of order be cause they are not in the list.   Unless these fields are truly different fields. Did you name a field as ""Group1.Check Box28#1"?

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

Translate
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 Beginner ,
Mar 01, 2018 Mar 01, 2018

Hi Thom

The two checkboxes are labelled "Group1.Check Box28", so that only one can be selected. They both have different export values and they do appear in the list. Is there another way to select one checkbox only out of two, if they are labelled with different names?

Translate
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 ,
Mar 01, 2018 Mar 01, 2018

By list, I mean the array returned by the "getArray()" function.

For example:

var aFldList = this.getField("Group1").getArray();

This list cannot contain special widget names.

The list displayed in right side panel in "Prepare Form" mode is irrelevant. It has quite literally nothing to do with the script you are trying to create.

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

Translate
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 Beginner ,
Mar 04, 2018 Mar 04, 2018

There are no special widget names in the list returned by the "getArray()" function. I can confirm that all checkboxes are labelled correctly and there no label names ending with #0 and #1. The problem is that the field which displays the export values of the selected checkboxes displays the widget export values at the end of the list.

The following shows a section of my form:

"Group1.Check Box 40" (Export value = "NSW Medal")

"Group1.Check Box 41" (There are 8 checkboxes with this name, but with different export values. "15 year clasp", "20 year clasp", "25 year clasp", "30 year clasp", "35 year clasp", "40 year clasp", "45 year clasp", "50 year clasp") Only one of these can be selected.

"Group1.Check Box 70" (Export value = "Olympic Citation")

"Group1.Check Box 71" (Export value = "Sesquicentenary Citation")

If I select these checkboxes, the field which displays the export values displays it like this:

NSW Medal

Olympic Citation

Sesquicentenary Citation

25 year clasp

The 25 year clasp should appear under NSW Medal (not at the end).

Please advise why this is happening.

Translate
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 ,
Mar 05, 2018 Mar 05, 2018

Very good. So all you have to do to get the order of the text showing correctly is to sort the array correctly.

Run this code in the console window to see how the array is being ordered.

var aFldList = this.getField("Group1").getArray().sort(function(a,b){return a.name<b.name;});

aFldList.forEach(function(a){console.println(a.name)});

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

Translate
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 Beginner ,
Mar 05, 2018 Mar 05, 2018

This is the result from the console window:

The numbers appear to be in order from 83 to 01.

var aFldList = this.getField("Group1").getArray().sort(function(a,b){return a.name<b.name;});

aFldList.forEach(function(a){console.println(a.name)});

Group1.Check Box83

Group1.Check Box82

Group1.Check Box81

Group1.Check Box80

Group1.Check Box79

Group1.Check Box75

Group1.Check Box74

Group1.Check Box73

Group1.Check Box72

Group1.Check Box71

Group1.Check Box70

Group1.Check Box66

Group1.Check Box62

Group1.Check Box58

Group1.Check Box50

Group1.Check Box49

Group1.Check Box41

Group1.Check Box40

Group1.Check Box39

Group1.Check Box38

Group1.Check Box34

Group1.Check Box30

Group1.Check Box28

Group1.Check Box27

Group1.Check Box26

Group1.Check Box25

Group1.Check Box24

Group1.Check Box23

Group1.Check Box22

Group1.Check Box21

Group1.Check Box20

Group1.Check Box19

Group1.Check Box18

Group1.Check Box17

Group1.Check Box16

Group1.Check Box15

Group1.Check Box14

Group1.Check Box13

Group1.Check Box12

Group1.Check Box11

Group1.Check Box10

Group1.Check Box09

Group1.Check Box08

Group1.Check Box07

Group1.Check Box06

Group1.Check Box05

Group1.Check Box04

Group1.Check Box03

Group1.Check Box02

Group1.Check Box01

undefined

Translate
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 ,
Mar 05, 2018 Mar 05, 2018

So it needs to be reversed. Use this code to get the list

var aFldList = this.getField("Group1").getArray().sort(function(a,b){return a.name>b.name;});

Then you can use this list to get the text.

Actually if you are really ambitious, you could use this code to get the complete value in one line

event.value = this.getField("Group1").getArray().filter(function(a){return a.value !="Off";}.sort(function(a,b){return a.name>b.name;}).map(function(a){return a.value}).join("\n");

The code first filters the array to only include fields that are checked, then sorts the order, then creates an array of just the export values, and then joins them into a multiline string.

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

Translate
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 Beginner ,
Mar 07, 2018 Mar 07, 2018
LATEST

Thank you Thom.

Your solution above has solved my problem. Appreciate your time and patience in answering my query.

Translate
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