Show and Hide Fields With Checkboxes
There are a few methods for showing and hiding fields with check boxes.
Check Box Field Basics
The value of a check box that is not checked is "Off". The value of a check box that is checked is its export value. The default export value when creating a check box field is "Yes". That means if you create a check box and don't change the export value (located in the Options tab of the field properties), the value will be Yes if checked and Off if not checked. These value strings are case sensitive.
If you have a series of check boxes with the same name, they must all be unchecked to return the Off value. If they all have different export values they will act like radio buttons where only one in the series can be checked at the same time.
I rarely use radio buttons because I can get the same affect of mutual exclusivity by naming check box fields identically with different export values. Once selected, a radio button series cannot be unselected without resetting the fields with a script. This is not the case with check boxes.
Any check box fields with the same name and export value will check and uncheck together. In other words, if you have two check boxes with the same name and export value and you check one, the other will check also. If you uncheck one, they will both uncheck. I created a tool for automating a series of mutually exclusive check boxes or radio buttons that will save you a lot of time.
The Mouse Up Method
When users click a check box (or press the space bar on the keyboard when the focus is on the check box), the value of the check box is the value it changes to when the user presses the mouse button and releases it up ("Mouse Up"). A Mouse Down action would be the opposite. That is, the value is the existing value when the mouse button is pressed before releasing it.
You can test this out by creating a check box field and passing its value into an alert box in a Mouse Up action.
To return the value of a check box in a mouse up action, use event.target.value NOT event.value.
Try it by creating a check box and entering the following script as Mouse Up action:
app.alert(event.target.value);
As you click the mouse the popup alert should alternate between Yes (when you check the box) and Off when you uncheck the box. Now change the export value to Hello in the options tab of the check box and try it again. It should alternate between "Hello" and "Off". You can also tab to the check box so it receives the focus and continue to press the space bar to obtain the same result.
Now assume you have a text field named Shipping Address that you want to hide when the check box is unchecked and show when the check box is checked. Use the following Mouse Up action:
var sa=this.getField("Shipping Address");
if(event.target.value=="Off")
{sa.display=display.hidden}
else
{sa.display=display.visible}
Another way to write the script above (or any if statement with only two conditions) is as follows:
var sa=this.getField("Shipping Address");
event.target.value=="Off"?sa.display=display.hidden:sa.display=display.visible;
The Calculation Method
event.target is the field containing the script. If you use a custom calculation script in the text field instead, the text field becomes event.target and the check box field needs to be named.
if(this.getField("Check Box1").value=="Off")
{event.target.display=display.hidden}
else
{event.target.display=display.visible}
The script above can also be written as follows:
this.getField("Check Box1").value=="Off"?event.target.display=display.hidden:event.target.display=display.visible;
Random Video
Today’s post was short so enjoy this video of Clutch playing Psychic Warfare at the Iron Horse Saloon in Sturgis South Dakota on Aug 6, 2023