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

Select and Deselect All Checkboxes

New Here ,
Nov 22, 2017 Nov 22, 2017

Hi,

I'm using Adobe acrobate dc.

I have three checkboxes in my pdf-document.

They are namned: checkbox1, checkbox2 and checkbox3

I wish the following to happen:

-When checkbox1 is selected, checkbox2 and checkbox3 also become selected.

-When checkbox1 is deselected, checkbox2 and checkbox3 also become deselected.

I'm more than a beginner to this with javascript, so please keep this in mind when

explaining or giving me a working javascript.

Thanks

/Dennis

TOPICS
Acrobat SDK and JavaScript , Windows
6.5K
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

correct answers 1 Correct answer

Community Expert , Nov 22, 2017 Nov 22, 2017

By select, do you mean checked? Selected means the field has keyboard focus.

Put your script on the MouseUp event for "checkbox1". All you need to do is set the value of the other checkboxes to the value of "checkbox1", which is "event.target.value.

So the line of code for setting "checkbox2" is:

this.getField("checkbox2").value = event.target.value;

So what happens when the user changes the value of "checkbox2"? Does it affect other fields?

Translate
Community Expert ,
Nov 22, 2017 Nov 22, 2017

By select, do you mean checked? Selected means the field has keyboard focus.

Put your script on the MouseUp event for "checkbox1". All you need to do is set the value of the other checkboxes to the value of "checkbox1", which is "event.target.value.

So the line of code for setting "checkbox2" is:

this.getField("checkbox2").value = event.target.value;

So what happens when the user changes the value of "checkbox2"? Does it affect other fields?

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
LEGEND ,
Nov 22, 2017 Nov 22, 2017

The following script will check the other 2 check boxes if box 1 is checked and uncheck them when box 1 is unchecked and it does not even need to know the value of the checked boxes.

// mouse up action JavaScript;

// get field objects;

var CB1 = this.getField("checkbox1");

var CB2 = this.getField("checkbox2");

var CB3 = this.getField("checkbox3");

// check check box 1 value;

if(CB1.value != "Off")

{

// check the ohter boxes;

CB2.checkThisBox(0,true);

CB3.checkThisBox(0,true);

} else {

// uncheck the other boxes;

CB2.checkThisBox(0, false)

CB3.checkThisBox(0, false);

}

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 ,
Nov 22, 2017 Nov 22, 2017

Got a lot of code there, why so much?

BTW: I corrected my previous post to use the correct event property for getting the value. It only takes two lines of code.  And it could be modified to use the "checkThisBox()" function if you wanted to avoid handling the export values.

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
New Here ,
Nov 23, 2017 Nov 23, 2017

Thank you Thom, the code

this.getField("checkbox2").value = event.target.value;

works fine and does its job.

As I'm a beginner of coding javascript and want to use this type of checkbox function on many locations and pages in my form, I would like to know what are the advantages and disadvantages of using export values instead of modifing it to use the "checkThisBox" instead?

Thanks a lot again!

/Dennis

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 ,
Nov 23, 2017 Nov 23, 2017

For most applications it doesn't matter if the code checks the export value or the checked state. I find the export value to be more convenient. And in this case it results in tighter code, which is always a good goal to have for a script.

However, testing for the checked state directly is more generic, if that is all you care about. One reason for doing this is for an automation script that needs to operate on checkboxes on any arbitrary form. In this situation you can't depend on the export value being the default "Yes".

It is quite rare that there is an advantage to modifying the checkbox export value to something other than "Yes". But these situations do come up, and when they do the export value is critical. For example, Radio-Checkboxes.

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
New Here ,
Nov 24, 2017 Nov 24, 2017

Okay interesting, thank you Thom. Just for the discussion (I don't want this for now so no script is needed. I also know that what I'm going to describe below is way to complicated for a rookie like me at this stage, but it's interesting to know if future needs will call):

Lets say that it's possible in a pdf-form that a log-in box appears every time you open this pdf-form. In the box you can choose to login in with different user names (e.g. userA, UserB etc).

Now, after loging in with a username, if a checkbox (lets say namned checkboxA) is checked then this triggers a text to be generated in a text field on the last page of the form that says e.g.: "checkboxA was checked by user X at date XX-XX-XXXX".

Would this type of script for checkboxA require that the code checks the checked state instead of the export value?

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 ,
Nov 24, 2017 Nov 24, 2017

Given what you've described, the code would be almost exactly the same as the code we've provided above.

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
New Here ,
Nov 25, 2017 Nov 25, 2017
LATEST

Thanks Thom for your answers helping me understand some more about javascripts.

Have a nice weekend!

Dennis

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
LEGEND ,
Nov 23, 2017 Nov 23, 2017

Sometimes commented and more obvious code might be more informative and maintainable.

I can modify the 2 fields as described with just 2 lines of code.

this.getField("checkbox2").checkThisBox(0, event.target.value != "Off");

this.getField("checkbox3").checkThisBox(0, event.target.value != "Off");

And there is no dependency on the export value of the fields.

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 ,
Nov 23, 2017 Nov 23, 2017

I don't disagree, however, I think you can be too verbose. I think beginners do best with tighter, but not cryptic code. Simple and to the point. Cause I think too much structure is distracting, and can be confusing as well.

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
New Here ,
Nov 24, 2017 Nov 24, 2017

Thanks gkaiseril for showing me another great way to do it.

/Dennis

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
New Here ,
Nov 23, 2017 Nov 23, 2017

Thank you gkaiseril for showing me how to do it with your code,

/Dennis

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