Skip to main content
Participating Frequently
April 25, 2022
Answered

JavaScript for radio buttons in Adobe Prepare Form

  • April 25, 2022
  • 2 replies
  • 4270 views

Hi

 

I could use some help with JavaScript in Adobe Prepare Form.

 

1)

I have a series of five radio buttons. For each of the five radio buttons there is a corresponding text field.

I would like the corresponding text field to change fill color when the radio button is selected, and then the fill color of the first text field should go back to transparent if the user changes their choice to one of the other radio buttons, and then the text field belonging to the newly selected radio button should change fill color instead.

I can get the text field to change fill color when the radio button is selected, but then when the choice is changed to a different radio button, the fill color in the first text field stays there. Can anyone help me fix the JavaScript to achieve this?

 

My current Java script to ”radio button, Choice1” is: this.getField("TextField1").fillColor = event.target.value == "Off" ? color.transparent : ["RGB", 253/255, 224/255, 195/255];

 

And for ”radio button, Choice2” the Javascript is: this.getField("TextField2").fillColor = event.target.value == "Off" ? color.transparent : ["RGB", 253/255, 224/255, 195/255];

 

And so on for the last three choices of the radio button as well.

 

But this script only changes the fill color when selecting the radio button (e.g. Choice1). When changing choice to a different radio button (e.g. Choice2) in the series of radio buttons, the fill color of the first text field does not go back to transparent. What is wrong with my script? Please help.

 

 

2)

Also, I would like the radio button itself to change fill color when the radio button is selected, and the fill color to go back to transparent again when a different radio button is selected instead. How do I do this?

I know how to do this for check boxes using this script: this.getField("CheckBox1").fillColor = event.target.value == "Off" ? color.transparent : ["RGB", 253/255, 224/255, 195/255];

But this script does not work for radio buttons it seems. Can anyone help fix this script as well?

 

Please see the attached for clarification.

 

Thank you. 

This topic has been closed for replies.
Correct answer try67

The solution is to adjust the code and move it to the calculation script of the text field itself.

 

What should the code for the text field be then? 

 

 


Something like this:

 

var rd = this.getField("RadioButton1").valueAsString;
if (rd == "Choice1") event.target.fillColor = ["RGB", 253/255, 224/255, 195/255];
else if (rd == "Choice2") event.target.fillColor = color.blue;
else if (rd == "Choice3") event.target.fillColor = color.red;
else if (rd == "Off") event.target.fillColor = color.transparent;

2 replies

JR Boulay
Community Expert
Community Expert
April 25, 2022

Place the same script in all radiobuttons, as a Mouse up action:

 

if (event.target.value == "Choice1") {this getField("TEXT").fillColor = ["RGB", 253/255, 224/255, 195/255];}

else if (event.target.value == "Choice2") {this getField("TEXT").fillColor = color.blue;}

else if (event.target.value == "Choice3") {this getField("TEXT").fillColor = color.red;}

// etc.

 

Since they are radio buttons (vs checkboxes) you don't have to manage the "Off" state.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
April 25, 2022

You do if you want to clear the text field's fill color when the form is reset, but in order to do that the MouseUp event won't work (since you can't "untick" a radio-button, as you correctly point out).

The solution is to adjust the code and move it to the calculation script of the text field itself.

 

PS. Your code is missing the opening parenthesis after all instances of getField ...

JR Boulay
Community Expert
Community Expert
April 25, 2022

"PS. Your code is missing the opening parenthesis after all instances of getField ..."

Fixed.

I should not write code too early in the morning.

Acrobate du PDF, InDesigner et Photoshopographe
Bernd Alheit
Community Expert
Community Expert
April 25, 2022

Where does you use the scripts?