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

Check and Uncheck a checkbox based on another checkbox status

Community Beginner ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

I have an Acrobat form with a checkbox that checks another checkbox.  I have used the mouseUP action in the original checkbox to Run a Javascript to check the 2nd checkbox which works.

If I uncheck the original checkbox, the 2nd checkbox still has a checkmark.  I have tried many solutions, but have not found success.

My mouseUP JS in the original checkbox is:

if(event.target.isBoxChecked(0))
this.getField("200Plus").checkThisBox(0,1);

 

Go easy on me, as I barely know how to spell Javascript.

Thank you.

TOPICS
Acrobat SDK and JavaScript

Views

8.0K

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

correct answers 3 Correct answers

Community Expert , Nov 01, 2020 Nov 01, 2020

Just name both checkbox same and leave them with same export value.

Votes

Translate

Translate
Enthusiast , Nov 01, 2020 Nov 01, 2020

And what happens if the user wants to have the ability to check or uncheck the secondary checkbox when the primary chexkbox is unchecked? Then why would he even need code in first place?

If you readed his post carefully you would see that he has no intention of clicking second checkbox.

To qoute OP "If I uncheck the original checkbox, the 2nd checkbox still has a checkmark. I have tried many solutions, but have not found success." in this situation why not just click 2nd box? This tells me he has

...

Votes

Translate

Translate
Community Expert , Nov 01, 2020 Nov 01, 2020

I closed Acrobat and restarted it.

 

I don't see where this script doesn't work.

 

  • First slide, boxes are checked with the console opened:

 

checkboxes.png

 

  • Second slide, boxes are unchecked with the console opened:

checkboxes2.png

 

Now I'm confused... should I be getting an undefined error? 

 

In any case I would expect a syntax error which actually happens when I add ".value" to the variable as you suggest:

 

var f = this.getField("200Plus").value;

 

In fact, I get a "Type Error" with your suggestion:

 

checkboxes3.png

 

Also note

...

Votes

Translate

Translate
Community Expert ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

This works for me:

 

 

if (event.target.value =="Yes") {

var f = this.getField("200Plus");

f.checkThisBox(0,true);

} else {

f.checkThisBox(0,false);

}

 

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 ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

This will not work. In the else part the variable f is undefined. You set the variable f only in the then part.

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

Thank you Bernd  but I tested this code and I don't get undefines error in the console because this script is not meant to obtain a value entered in this field. 

 

As explained by George Kaiser in this quick tutorial:

 

https://answers.acrobatusers.com/How-do-I-reference-a-field-through-javascript-q192043.aspx 

 

using this.getField("myCheckBox")   , the script expects to get the field object (in this case a checkbox widget), by which it will return the field object as the variable "oField".  

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

Close and restart Acrobat. Then test the form again.

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

I closed Acrobat and restarted it.

 

I don't see where this script doesn't work.

 

  • First slide, boxes are checked with the console opened:

 

checkboxes.png

 

  • Second slide, boxes are unchecked with the console opened:

checkboxes2.png

 

Now I'm confused... should I be getting an undefined error? 

 

In any case I would expect a syntax error which actually happens when I add ".value" to the variable as you suggest:

 

var f = this.getField("200Plus").value;

 

In fact, I get a "Type Error" with your suggestion:

 

checkboxes3.png

 

Also note that this breaks the original intent of the script as requested by the OP since the primary checkbox looses the ability to check the secondary checkbox.

 

In the end, I am running the script like shown below (which is not throwing any errors and there's no need to define or even declare a variable using the guidance posted by George Kaiser in the link that I posted earlier):

 

if (event.target.value =="Yes") {

this.getField("200Plus").checkThisBox(0,true);

} else {

this.getField("200Plus").checkThisBox(0,false);

}

 

 

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 Beginner ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

Hello!  This script was almost exactly what I was looking for and worked as expected!  However, I wonder if you could take this one step further.  I need the "slave" checkbox to stay checked if any (one or all) of the master boxes are checked and only turn off if NONE of them are checked.  As it stands now, if I uncheck any of the "master" checkboxes the slave box unchecks even if one or more of the master boxes is still checked.  Did I explain that well enough?  Thank you!

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 Beginner ,
Dec 13, 2022 Dec 13, 2022

Copy link to clipboard

Copied

ls_rbls...  I am re-posting this because I think I accidently posted under one of the replies instead of here.

Hello!  This script was almost exactly what I was looking for and worked as expected!  However, I wonder if you could take this one step further.  I need the "slave" checkbox to stay checked if any (one or all) of the master boxes are checked and only turn off if NONE of them are checked.  As it stands now, if I uncheck any of the "master" checkboxes the slave box unchecks even if one or more of the master boxes is still checked.  Did I explain that well enough?  Thank you!

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
New Here ,
Mar 28, 2023 Mar 28, 2023

Copy link to clipboard

Copied

Is "200Plus" your "slave" checkbox?

Your OP says you want the 1st to mark the 2nd, and the imply you want the 2nd unmarked when the 1st becomes unmarked.

Here, you seem to clarify that you want the "slave" 2nd checkbox to unmark only if all "master"s are unchecked.

 

If you want the "master" checkboxes to only add a checkmark to the "slave" checkbox(es), sounds like omitting the else portion of code -- which would result in the "master" checkboxes only being able to add a checkmark, but not remove it -- might be an acceptable simple solution?

 

To remove the checkmark if ALL "master" checkboxes are unchecked, I'm afraid you have some work ahead of you.  You'd have to test the value of every "master" checkbox in the else portion of code, something like:

var fSlave = this.getField("SlaveCheckbox");
var f1 = this.getField("MasterCheckbox1");
var f2 = this.getField("MasterCheckbox2");
var f3 = this.getField("MasterCheckbox3");
//  add more as needed

if(f1.value=="Off" & f2.value=="Off" & f3.value=="Off") {
    //uncheck target "slave"
    fSlave.checkThisBox(0,false);
}

 

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

Just name both checkbox same and leave them with same export value.

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

And what happens if the user wants to have the ability to check or uncheck the secondary checkbox when the primary chexkbox is unchecked?

 

If the case would be that the user needs either the primary or the secondary box checked at a time, then it should be better to implement a pair of mutually exclusive radio buttons that will behave like checkboxes.

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
Enthusiast ,
Nov 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

And what happens if the user wants to have the ability to check or uncheck the secondary checkbox when the primary chexkbox is unchecked? Then why would he even need code in first place?

If you readed his post carefully you would see that he has no intention of clicking second checkbox.

To qoute OP "If I uncheck the original checkbox, the 2nd checkbox still has a checkmark. I have tried many solutions, but have not found success." in this situation why not just click 2nd box? This tells me he has none intention of clicking it. Easiest solution would be to just copy first box.

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 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

Yes you're correct. But this is one of those threads where the user needs to clarify his intent because it is still ambiguous.

 

Note that both of us have interpreted the user's intent differently. So it needs to be clarifies by the user.

 

Like, for instance, I would like in my PDF to be able to have the ability to check or uncheck the secondary checkbox independently from the checked state of the primary checkbox, specially if the primary checkbox is on the first page of a document but the secondary checkbox is on page 10 for example.

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
New Here ,
Oct 21, 2023 Oct 21, 2023

Copy link to clipboard

Copied

LATEST

just have a look this video:

https://youtu.be/IS2Bs6KXhyE

 

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