Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

If radio button is selected then text field shows up

New Here ,
Mar 16, 2017 Mar 16, 2017

Hello,

I'm brand spanking new to JavaScript and Adobe form fields, so I'm sure this is a very basic question. I am making a form which has two radio buttons "Option 1" and "Other (Please Specify)". I want the form to make a text field appear (and become required if possible) if someone selects the "Other (please specify)" radio button, but I don't want it to show up as an option if they picked "Option 1".

What is the best way to do this?

Thanks for your help.

TOPICS
PDF forms
6.8K
Translate
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 Expert ,
Mar 17, 2017 Mar 17, 2017

Let's say the "radio button choice" values of these two options are "1" (for "Option 1") and "2" (for "Other"), and that the name of the text field is "Other". In that case, use this code as the Mouse Up event of both radio-button widgets:

var otherField = this.getField("Other");

if (event.target.value=="2") {

    otherField.display = display.visible;

    otherField.required = true;

} else {

    otherField.display = display.hidden;

    otherField.required = false;

    otherField.value = "";

}

The only problem with this approach is that if the user resets the form while option 2 is selected the code is not triggered and the text field remains visible (and required)... To solve that you would need to use the calculation script of a (hidden) text field to trigger the code.

Translate
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
New Here ,
Mar 23, 2023 Mar 23, 2023

Can you provide the calculation script of the hidden text field for this scenario?  Also, how would the script(s) be enhanced so that if  Option 2 "Other (please specify)" were selected and text were entered in the "Other" text field, but then the user changed their mind and selected Option 1, the result would be the text field would clear and be hidden again.

Translate
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 Expert ,
Mar 23, 2023 Mar 23, 2023
LATEST

Let's say radio button field name is "Group1", as custom calculation script of "Other" text field use this:

var f = this.getField("Group1").valueAsString;
if(f == "Other (please specify)")
event.target.display = display.visible;
else{
event.target.display = display.hidden;
event.value = "";}

Translate
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