Select and Deselect All Checkboxes

New Here ,
Nov 22, 2017

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.6K

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

Select and Deselect All Checkboxes

New Here ,
Nov 22, 2017

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.6K

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
Nov 22, 2017 0
Adobe Community Professional ,
Nov 22, 2017

Copy link to clipboard

Copied

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?

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...
Nov 22, 2017 0
Adobe Community Professional ,
Nov 22, 2017

Copy link to clipboard

Copied

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);

}

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...
Nov 22, 2017 0
Adobe Community Professional ,
Nov 22, 2017

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

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...
Nov 23, 2017 0
Adobe Community Professional ,
Nov 23, 2017

Copy link to clipboard

Copied

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.

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...
Nov 23, 2017 1
Adobe Community Professional ,
Nov 23, 2017

Copy link to clipboard

Copied

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.

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...
Nov 23, 2017 2
Adobe Community Professional ,
Nov 23, 2017

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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?

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

Copy link to clipboard

Copied

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

/Dennis

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...
Nov 24, 2017 0
Adobe Community Professional ,
Nov 24, 2017

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

Have a nice weekend!

Dennis

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

Copy link to clipboard

Copied

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

/Dennis

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