Skip to main content
Known Participant
May 12, 2016
Answered

New User - needs script help

  • May 12, 2016
  • 1 reply
  • 603 views

Hi folks,  new user here. 

Have a text field that I would like to be hidden until one of two radio buttons in a group has been selected.  All help greatly appreciated. 

Rick S/San Antonio, TX

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

You can do this in different ways. Here is one example. Use the following as the custom calculation script in your text field that should be hidden or shown:

var f1 = this.getField("Group1");

var shouldBeVisible = (f1.value != "Off");

event.target.display = shouldBeVisible ? display.visible : display.hidden;

This can be shortened a bit, but shorter code is not necessarily more readable. If you are having problems understanding what's going on here, please review the JavaScript core language syntax. What you need to know is that a radio button group that has no selection will report "Off" as it's value. So the field should be visible when the radio button group is not set to "Off". In that case, we set the event.target.display property (you can find what that means in the Acrobat JavaScript API documentation: Acrobat DC SDK Documentation: event.target ) to display.visible or to display.hidden in the other case (again, more information is in the API documentation).

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
May 12, 2016

You can do this in different ways. Here is one example. Use the following as the custom calculation script in your text field that should be hidden or shown:

var f1 = this.getField("Group1");

var shouldBeVisible = (f1.value != "Off");

event.target.display = shouldBeVisible ? display.visible : display.hidden;

This can be shortened a bit, but shorter code is not necessarily more readable. If you are having problems understanding what's going on here, please review the JavaScript core language syntax. What you need to know is that a radio button group that has no selection will report "Off" as it's value. So the field should be visible when the radio button group is not set to "Off". In that case, we set the event.target.display property (you can find what that means in the Acrobat JavaScript API documentation: Acrobat DC SDK Documentation: event.target ) to display.visible or to display.hidden in the other case (again, more information is in the API documentation).

Known Participant
May 13, 2016

Hi again,

First... thank you for such a quick response... what a refreshing change !

Second... I did not pose my question with sufficient clarity.. so allow me to elaborate:

I have two radio buttons in a group:  Group Name is:  "English_YN", and the buttons are named "English-Yes"  and  "English-No".  What I'd like to be able to do is keep another field called  "Alt Language"  hidden unless the  "English-No"  radio button has been clicked.

So,

     if the radio button named  "English-Yes" is selected, then  "Alt Language" remains hidden, and can not be populated.

     if the radio button named  "English-No"  is selected, then  "Alt Language"  is revealed and can be populated with the appropriate      response.

I have coded in other languages, but am a  COMPLETE BEGINNER  in anything resembling JavaScript... I can sort of understand code that already exists, but have no  new code development skill as of yet.   As mentioned earlier, all assistance greatly appreciated !!

Many thanks & till later,

Rick Stockstill

San Antonio, TX

Karl Heinz  Kremer
Community Expert
Community Expert
May 13, 2016

I went down a different path with my script. You can accomplish your goals with a slight variation on what I posted yesterday. See here for a script that should work for you:

var f1 = this.getField("English_YN"); 

var shouldBeVisible = (f1.value == "English-No"); 

event.target.display = shouldBeVisible ? display.visible : display.hidden; 

As you can see, the structure of the script is the same, the big difference is in the comparison operation. We now know what button needs to be active in order to activate the text field. This again should be used as the custom calculation script for the "Alt Language" field.