Highlighted

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

Explorer ,
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

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

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

}

TOPICS
PDF forms, Windows

Views

3.8K

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

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

Explorer ,
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

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

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

}

TOPICS
PDF forms, Windows

Views

3.8K

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

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...
Mar 08, 2018 2
Explorer ,
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

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

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

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...
Mar 12, 2018 0
Adobe Community Professional ,
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";

}

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

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...
Mar 13, 2018 1
Community Beginner ,
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

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

Copy link to clipboard

Copied

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

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...
Dec 14, 2018 0
Community Beginner ,
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

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

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...
Dec 18, 2018 0
Community Beginner ,
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

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

Copy link to clipboard

Copied

Give them all the same name but different 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...
Dec 18, 2018 0