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

Explorer ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

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

TOPICS
PDF forms, Windows

Views

4.5K

Likes

translate

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 1 Correct Answer

Adobe Community Professional , Mar 12, 2018 Mar 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 uncheckedNotice that the export values for 2 & 3 are not used, since they are only being turned ...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 08, 2018 Mar 08, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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
Explorer ,
Mar 09, 2018 Mar 09, 2018

Copy link to clipboard

Copied

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.

Likes

translate

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
Adobe Community Professional ,
Mar 09, 2018 Mar 09, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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
Explorer ,
Mar 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

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

Likes

translate

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
Adobe Community Professional ,
Mar 12, 2018 Mar 12, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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
Explorer ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

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.

Likes

translate

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
Explorer ,
Apr 18, 2021 Apr 18, 2021

Copy link to clipboard

Copied

Thank You. Very useful code.

Likes

translate

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 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

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

Likes

translate

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
Most Valuable Participant ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

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

Likes

translate

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 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

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

Likes

translate

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
Adobe Community Professional ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

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

Likes

translate

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
Most Valuable Participant ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

Give them all the same name but different export values.

Likes

translate

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 ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

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

Likes

translate

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
Adobe Community Professional ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

You can find it in field properties action tab:

Izrezak.PNG

Likes

translate

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 ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

Thanks!

Likes

translate

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
Most Valuable Participant ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

Go to Tools - Forms - Prepare Form and then right-click a field, select Properties and look under the Actions tab.

Under Trigger select "Mouse Up" and under Action select "Run a JavaScript". Then click Add, paste the code into the editor window that opens up and you're done!

Likes

translate

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 ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

Thanks, I got it 🙂

Likes

translate

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