Radio Button color change JavaScript - To change based on selection
Copy link to clipboard
Copied
I have a Radio Button Group with 2 choices(Group1) - a Yes and a No radio buttons. I would like for the color to change based on the which one is selected. When the No button is check, it should be Green and Yes should be white. When the Yes button is selected, it should be Red and the No should be white.
I have this currently entered in the mouse up
event.target.fillColor = event.target.value=="Off" ? color.white : color.red;
But it changes both of the buttons. ot the color choice of the button that is chosen.
Any help would be appreciated. Thank you!
Copy link to clipboard
Copied
I recently answered a similar question about check-boxes, but the same applies for radio-buttons.
See: https://community.adobe.com/t5/acrobat/different-fill-colors-for-same-name-check-boxes/m-p/11383110
Copy link to clipboard
Copied
Thank you so much! I did actually search, but I couldn't find the "same" button thing.
Copy link to clipboard
Copied
So I researched the post you recommended and have been trying to apply the code you have...
"To do it you would need to access the individual widget, which event.target doesn't do. So you would need to use something like this:
this.getField("CheckBox1.0").fillColor = (this.getField("CheckBox1").value=="Yes") ? color.green : color.transparent;
this.getField("CheckBox1.1").fillColor = (this.getField("CheckBox1").value=="No") ? color.yellow : color.transparent; "
My problem is my naming conventions do not match above and I have not been able to figure it out.
My Radio Button is named "Group15" with 2 buttons named YES and NO I do not see where to add the .0 and .1 as indicated in the above script.
Copy link to clipboard
Copied
Simply use:
this.getField("Group15.0").fillColor = (this.getField("Group15").value=="YES") ? color.green : color.transparent;
this.getField("Group15.1").fillColor = (this.getField("Group15").value=="NO") ? color.yellow : color.transparent;
Copy link to clipboard
Copied
LOL...Thank you!! I was trying to add the .0 to both fields.
Thank you again for being so quick to answer!!
Copy link to clipboard
Copied
Did you manage this to work? I could use same code but I can't get it to work.
I named my radio buttons "Group15.0 and Group15.1" I gave 15.0 value Yes and 15.1 value No.
I use code in both buttons as mouse up event but they don't change color and they don't act as radio buttons anymore. Do I use all code in both buttons or first part in 15.0 button and second part in 15.1 button?
Pls help I'm not sure what I'm doing wrong here.
Copy link to clipboard
Copied
Do not name them like that. Just use "Group15". The ".0" and ".1" parts are added automatically, behind the scenes.
Copy link to clipboard
Copied
Thanks I fixed that now they act as radio buttons again, but can you help one more time pls, buttons don't change colors, they have export values of yes and no, I use codes as mouse up event. I probably still missing something just can't figure out what it is.
Copy link to clipboard
Copied
Is it "yes" and "no", or "YES" and "NO"? The two are not the same... For further help you will need to share your file with us.
Copy link to clipboard
Copied
Thanks, that was it, I had Yes instead of YES.

