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
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
...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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Give all those fields the same name, but unique export values, and it will work like that automatically.
Copy link to clipboard
Copied
Thank you so much for the prompt feedback. Would you be able to help with an example?
If I saw it written out it would really help. When you say "Give all those fields the same name" do you mean like this where each field matches the check box name?
var bChecked = (event.target.value == "Yes");
this.getField("Black Checkbox").value = bChecked?"Off";
this.getField("White Checkbox").value = bChecked?"Off";
this.getField("Alpha").value = bChecked?"Off";
Im on the "Blue Checkbox" property...in english I think this is saying
Here is an action to execute "var" (if the event on the property is YES) then GET the other checkboxes (FIELDS) and see if they say YES and of so turn them off?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Read This:
https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm
Copy link to clipboard
Copied
Thanks Thom, I read this through but I just dont have the foundtions to do this and apply these learnings. I did just start learning if/else conditions but was able to find a video tut that helped explain the logic and walk you thru the syntax. Ill just have to keep poking around until I can find some videos that can help me watch and learn.
best, -z
Copy link to clipboard
Copied
As try67 stated, you don't need any scripting to make a set of Checkboxes mutually exclusive. He explained how to do this in his post, and it's explained in the article. Look at the section labeled "Radio-Checkbox"
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.
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?
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).
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";
}
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.
Copy link to clipboard
Copied
Thank You. Very useful code.
Copy link to clipboard
Copied
Why is this not working on mobile? It is working on computer but not on mobile. Please help
Copy link to clipboard
Copied
Mobile PDF applications have very little to no support for JavaScript, unfortunately.
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
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.
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
Copy link to clipboard
Copied
Give them all the same name but different export values.
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
Copy link to clipboard
Copied
You can find it in field properties action tab:
Copy link to clipboard
Copied
Thanks!
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!