Skip to main content
Participating Frequently
May 23, 2020
Answered

Enable CheckBoxes when CheckBox 1 is checked.

  • May 23, 2020
  • 3 replies
  • 9521 views

Dear All,

 

I need your great help on Adobe Acrobat .

 

I have CheckBox 1. And another 4 checkboxes which I like to classify them as Other CheckBoxes.

 

I need to do the following:

Other CheckBoxes are only enable when CheckBox 1 is checked. If CheckBox 1 not checked, the Other CheckBoxes cannot be checked.

For Other CheckBoxes, only one of the 4 checkboxes can be checked.

 

Thank you. Appreciate your guidance here.

 

This topic has been closed for replies.
Correct answer Old_Salt

Sorry about that.

To get the behavior you're looking for:

Give the main check box its own name:
My Example: CheckBox


Give the other checkboxes the same name. The resulting names will be:
My example: Choice1#0, Choice1#1, Choice1#2, Choice1#3

These become a part of a group by virtue of having the same name (with#n appended to the name) and allows for only one to be checked just like a radio button.

 

Each of these checkboxes need to have a different export value:
Choice1#0 = "Yes1", Choice1#1 = "Yes2", Choice1#3 = "Yes3", Choice1#4 = "Yes4"

 

Rather than checking the boolean value of the main checkbox, just get its export value, in this case "Yes".

 

Here's the code I used:
var CheckBoxVal = this.getField("CheckBox").value;

if (CheckBoxVal == "Yes") {
this.getField("Choice1").readonly = false;
} else {
this.getField("Choice1").readonly = true;
}

 

Hope this helps.

3 replies

boilermaker73
Inspiring
May 3, 2021

@steven11980

To begin, I came across this post in the Adobe forum by chance in performing an online search looking for an answer to something else totally unrelated. Anyway, in reading your question along with the answers provided at the time, I was  totally amazed in that none of the answers mentioned the 'isCheckBox' method. Needless to say, this method came immediately to mind in reading your question. To employ this method, create one checkbox named 'checkBox' and four (4) checkboxes each with the same name 'chkBox' and assign a different export value, i.e., A, B, C, and D to each. Next, in the mouse up event for checkbox named 'checkBox' add the following script where event.target refers to the field 'checkBox'. This code will do what you want once the checkBox (same as CheckBox1 in your initial question) has been checked. In other words, once checked the remaining (4) checkboxes will be rendered read-only or unable to be checked. If unchecked, you will only be able to check one of the four checkboxes as needed. IMO, the first answer provided exemplifes making a mountain out of a mole hill.

f=getField("chkBox")
if(event.target.isBoxChecked(0))
f.readonly=true
else
f.readonly=false

boilermaker73
Inspiring
May 3, 2021

Sorry, in my previous answer I forgot to mention that when you create the four checkboxes with the same name 'chkBox' and different export values, you need to check/assign 'read-only' in the field property's box under 'general'. Also, as for the script in the mouse-up event for your Check Box 1, since you only want to be able to check the other boxes if Check Box 1 is checked, the script should read:

f=getField("chkBox")
if(event.target.isBoxChecked(0))
f.readonly=false
else
f.readonly=true

 

 

Thom Parker
Community Expert
Community Expert
May 3, 2021

The "isBoxChecked" solution is functionally equivelant in this case, however, I never use this function because the widget number input renders it non-unversal.  There is no way programatically to know the widget number of the field the script is operating on. In this scenario you are assuming there is only one widget because there is one checkbox, but this is not always true. Its best to stick with the simplest generic solution.  the statement, event.target.value!="Off", does not depend on widgets or export values. It is the most universal solution.     

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
May 23, 2020

Give all the "Other"checkboxes the same name, but different export values. This makes them a mutually exclusive group.

Next add this code to a MouseUp "Run a JavaScript" action on the first checkbox

 

this.getField("OtherCheckbox").readOnly = (event.target.value == "Off");

 

See this article for info on scripting checkboxes/radio buttons.

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
May 24, 2020

Hi Thom,

 

Thanks for the prompt reply. My apologies, your suggestion does not work as expected. The end result as follows: If  "CheckBox 1" is check, the "Other CheckBoxes" is checkable; If  "CheckBox 1" is uncheck, the "Other CheckBoxes" is still checkable. Prior to posting here, I tried what you have proposed.

 

The closest I could get to was:

For the "Other CheckBoxes", I have named them as "OtherCheckBox" and assigned a different export value 1, 2, 3 and 4 respectively.

For CheckBox 1, I have added a script as below:

// set readonly property based on the value of this field compared to "Off";

this.getField("OtherCheckBox").readonly = (event.target.value != "Off");

The end result as follows: If  "CheckBox 1" is check, the "Other CheckBoxes" is uncheckable; If  "CheckBox 1" is uncheck, the "Other CheckBoxes" is uncheckable.

 

Would you be able to advise again? Thank you.

 

 

Thom Parker
Community Expert
Community Expert
May 29, 2020

The script works as specified, so something else is the problem. You'll need to do some debug.

Is anything reported in the console window?

 

Add a console.println() statement to the sdript to display the value of "event.target.value"

 

you'll find a tutorial on the console window here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Old_Salt
Participating Frequently
May 23, 2020

I see this behavior if you do a copy and paste of any kind of form field. If you copy and paste the pasted field names will be the same except the field name will be appended with a # and a unique character. you'll need to make sure the field names are unique without the # extension. 

Participating Frequently
May 24, 2020

Hi rjunke,

 

Not very clear what you are saying. Can you elaborate? Thank you.

Old_Salt
Old_SaltCorrect answer
Participating Frequently
May 24, 2020

Sorry about that.

To get the behavior you're looking for:

Give the main check box its own name:
My Example: CheckBox


Give the other checkboxes the same name. The resulting names will be:
My example: Choice1#0, Choice1#1, Choice1#2, Choice1#3

These become a part of a group by virtue of having the same name (with#n appended to the name) and allows for only one to be checked just like a radio button.

 

Each of these checkboxes need to have a different export value:
Choice1#0 = "Yes1", Choice1#1 = "Yes2", Choice1#3 = "Yes3", Choice1#4 = "Yes4"

 

Rather than checking the boolean value of the main checkbox, just get its export value, in this case "Yes".

 

Here's the code I used:
var CheckBoxVal = this.getField("CheckBox").value;

if (CheckBoxVal == "Yes") {
this.getField("Choice1").readonly = false;
} else {
this.getField("Choice1").readonly = true;
}

 

Hope this helps.