Skip to main content
Inspiring
March 8, 2018
Answered

Checkbox checked, then two other checkboxes become both unchecked and disabled/read-only

  • March 8, 2018
  • 6 replies
  • 24910 views

Hi

In my pdf form I have three checkboxes named: checkbox1, checkbox2 and checkbox3.

I wish the following to happen:

If checkbox1 is checked, then checkbox2 and checkbox3 become unchecked/cleared (if they were previously checked), but at the same time checkbox 2 and checkbox3 also become disabled/read-only (by disabled I mean not checkable, not clickable, just read-only).

I have a javascript that does kind of the opposite (which I earlier got help with on this forum on a similar but different case).

It looks like this: checkbox1 checked then checkbox2 and checkbox 3 become checked and disabled/read-only.

And the script for this looks like this:

on the MouseUp script for "checkbox1".

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

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

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

this.getField("checkbox3").readonly = (event.target.value == "Yes");

So as a rookie I have tried to experiment with this script as a base but no solution so far.

Appreciate your help on this

/Dennis

This topic has been closed for replies.
Correct answer Thom Parker

It is important to work through the logic.

When you say that checking 1 causes 2 to become unchecked if it was checked, but doesn't do anything if 2 is already unchecked. The result is that checking 1 always unchecks 2.  This is an important simplification when you go to write the code.

So here is a variation that only changes 2 & 3 (to unchecked) when 1 is checked, but leaves them untouched when 1 is unchecked

Notice that the export values for 2 & 3 are not used, since they are only being turned off (unchecked).  The code never checks 2 & 3.

var bChecked = (event.target.value == "Yes")

this.getField("checkbox3").readonly = bChecked;

this.getField("checkbox2").readonly = bChecked;

if(bChecked)

{

     this.getField("checkbox3").value = "Off";

     this.getField("checkbox2").value = "Off";

}

6 replies

New Participant
September 25, 2020

Hi experts,
I can understand the code that is posted in this question. But, I am struggeling the step before.
dennisg96201989 wrote that the code should be pasted in "MouseUp script". Where can I find it?
I am working on a PDF form with Adobe Acrobat DC. Which tool do I have to use and where to put the code generally/in specific field settings?
Thanks for your help!
Regards, Deborah

Nesa Nurani
Community Expert
September 25, 2020

You can find it in field properties action tab:

New Participant
September 25, 2020

Thanks!

Known Participant
December 18, 2018

I have more than 20 checkboxes and I want when I check one checkbox other checkbox should get uncheck automatically. At a time user should only be allowed to check one checkbox.

Please help

Thom Parker
Community Expert
December 18, 2018

You mean it isn't working on mobile?  As Try67 states, most mobile PDF viewers do not implement JS.  Or they do it badly. However, simple form code such as that shown in my post should work on the Mobile Adobe Reader and the Readdle PDF Expert. If you're having problems, then you need to create a simple test to figure out what works and what doesn't.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
December 18, 2018

Can I get code for the following:

I have more than 20 checkboxes and I want when I check one checkbox other checkbox should get uncheck automatically. At a time user should only be allowed to check one checkbox.

Please help

Known Participant
December 14, 2018

Why is this not working on mobile? It is working on computer but not on mobile. Please help

try67
Community Expert
December 14, 2018

Mobile PDF applications have very little to no support for JavaScript, unfortunately.

Inspiring
March 12, 2018

Ok, I see. I would gladly learn how do it in a better way.

I want 2 & 3 to become unchecked (if they were previously checked. But if they hadn't been previously checked, they will remain unchecked) when 1 is checked. But at the same time, when 1 is checked, 2 and 3 also become disabled/read-only (not checkable, not clickable, just read-only).

Then if 1 is unchecked again, 2 & 3 become enabled (checkable/clickable) again. By enabled I just mean that it becomes possible to check/click 2 & 3 again (not that unchecking 1 will result in that 2 & 3 become checked).

Thom Parker
Thom ParkerCorrect answer
Community Expert
March 12, 2018

It is important to work through the logic.

When you say that checking 1 causes 2 to become unchecked if it was checked, but doesn't do anything if 2 is already unchecked. The result is that checking 1 always unchecks 2.  This is an important simplification when you go to write the code.

So here is a variation that only changes 2 & 3 (to unchecked) when 1 is checked, but leaves them untouched when 1 is unchecked

Notice that the export values for 2 & 3 are not used, since they are only being turned off (unchecked).  The code never checks 2 & 3.

var bChecked = (event.target.value == "Yes")

this.getField("checkbox3").readonly = bChecked;

this.getField("checkbox2").readonly = bChecked;

if(bChecked)

{

     this.getField("checkbox3").value = "Off";

     this.getField("checkbox2").value = "Off";

}

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
March 13, 2018

Thanks for clearifying how to think and simplify when writing code, it´s of great value and interesting to me as a beginner to learn some of the basics.

The code works very well and does it's job. In addition your code automatically solves another issue in a part in my form were I use "yes" or "no" checkboxes. Only one choice is possible, yes or no, and at the same time it’s possible to uncheck (undo) the made choice. For this I named the checkboxes with the same name and then entered different export values. Now when the script acts on those checkboxes (& doesn’t use export values), they function as intended.

Thanks a lot Thom, really appreciate your help and beginner-friendly explanations.

Inspiring
March 9, 2018

This script does  exactly what I wished for, thanks a lot Thom!

I can see setting all checkboxes to export value "Yes" can come in handy in various situations.

In my case I set the export value for checkbox1 to be "Yes" and for checkbox2 and checkbox3 to be "No", because I need those checkboxes to still be unchecked if checkbox1 is unchecked.

Thom Parker
Community Expert
March 9, 2018

It's better to set the export values to what you need them to be, and then adjust the code to use the specific values.

So you want 2 & 3 to be checked when 1 is checked, but not to turn off when 1 is unchecked?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
March 8, 2018

You just need to switch the script around a bit

var bChecked = (event.target.value == "Yes")

this.getField("checkbox2").value = bChecked?"Off":"Yes";

this.getField("checkbox2").readonly = bChecked;

this.getField("checkbox3").value = bChecked?"Off":"Yes";

this.getField("checkbox3").readonly = bChecked;

Assuming the export value for all checkboxes is "Yes", this script sets the other fields to the opposite value of Checkbox1 and makes them read only when checked

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
New Participant
February 15, 2022

Hello! Im very new to expressions. Im trying to repurpose this code but to no avail.

I have 4 check boxes. "Blue Checkbox" ,  "Black Checkbox" ,  "White Checkbox"  and "Alpha".

Simply, whenever one is checked all others are unchecked, thats it.

Any help would be very appreciated!


try67
Community Expert
February 15, 2022

Give all those fields the same name, but unique export values, and it will work like that automatically.