Skip to main content
scotwllm
Inspiring
November 13, 2019
Question

Changing the text in a form field based on choosing a radio button

  • November 13, 2019
  • 2 replies
  • 2415 views

Hello Adobe Geniuses!

 

I have a group of radio buttons:

  • Button 1
  • Button 2
  • Button 3

The group's name is radioButtons

 

I have a form field:

myFormField

 

If a user chooses Button 1, I would like the text in myFormField to change to "Button 1"

If a user chooses Button 2, I would like the text in myFormField to change to "Button 2"

If a user chooses Button 3, I would like the text in myFormField to change to "Button 3"

 

How would I do that?

 

Scott

This topic has been closed for replies.

2 replies

scotwllm
scotwllmAuthor
Inspiring
November 14, 2019

When I click on a radio button, text should appear in the form field, correct? Nothing seems to be happening.

ls_rbls
Community Expert
Community Expert
November 14, 2019

Did you changed anything in the script that I pasted here?

 

I found another small mistake but it is working on my end.

 

If you re-tryped fieldnames in the scripts make sure it is spelled the same way or if you are missing something.

 

Did you pasted the text that you want it to be displayed in the export value blank shown in the second slide above?

Thom Parker
Community Expert
Community Expert
November 14, 2019

You shouldn't have added the reset, it just complicates the script. Keep it simple.

Use only this code, nothing else:

 

event.value = this.getField("Button").valueAsString;

 

You don't need to handle reset in the calculation script.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ls_rbls
Community Expert
Community Expert
November 13, 2019

Hi,

 

You need to rename each radio button with the same fieldname. Just change the name to "Button" for each radio button, for example. But add a unique export value for button 1, button 2, and button 3 respectively.

 

 

Type in the text that you want it to be displayed in myFormField as the in each of the radio buttons' export value.

 

To do this, in Edit mode, right click on the radio button(s), select Properties---->>> Options and type in your text in the provided blank for the export field. Make sure to uncheck the option that says button is checked by default.

 

 

Then in myFormField textfield open field Properties, goto the Calculate tab, and add a script as Custom Calculation script like this:

 

var button = this.getField("Button").valueAsString;

if (button=="") event.value = "";

else event.value = button;

 

 

scotwllm
scotwllmAuthor
Inspiring
November 14, 2019

Hi rbls --

 

Thank you for responding. I did as you suggested, except I named the group HealthPlan instead of Button. After I type the code into the Javascript editor and press "OK," I get the following error message:

 

SyntaxError: missing; before statement

2: at line 3

 

Here is my code:

 

var button = this.getField("HealthPlan").valueAsString;
If (button=="") event.value = "";
else event.value = button;

 

Any idea what the problem is?

ls_rbls
Community Expert
Community Expert
November 14, 2019

Yes, I apologize. I made a mistake.

 

Use the code like this:

 

var button = this.getField("HealthPlan").valueAsString;
if (reset=="") event.value = "";
else event.value = button;


//if you need to clear or reset the radio button selection after the user ticked on one of the option add this line at the end. Remove the slashes // if you are using it

 

//this.resetForm(["HealthPlan"]) ;

 

//The script above was taken from here https://answers.acrobatusers.com/clearing-marked-radio-buttons-form-q1600.aspx

 

 

 

 

Also review the slides below: