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

JavaScript for radio buttons in Adobe Prepare Form

New Here ,
Apr 25, 2022 Apr 25, 2022

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. 

TOPICS
How to , JavaScript , PDF forms
4.1K
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
1 ACCEPTED SOLUTION
Community Expert ,
Apr 25, 2022 Apr 25, 2022
LATEST

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;

View solution in original post

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 ,
Apr 25, 2022 Apr 25, 2022

Where does you use the scripts?

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 ,
Apr 25, 2022 Apr 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
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 ,
Apr 25, 2022 Apr 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 ...

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 ,
Apr 25, 2022 Apr 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
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 ,
Apr 25, 2022 Apr 25, 2022

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? 

 

 

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 ,
Apr 25, 2022 Apr 25, 2022
LATEST

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;
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