Skip to main content
Known Participant
August 17, 2022
Answered

Yes/No Radio Button Groups that relate to the same fields showing/hiding

  • August 17, 2022
  • 1 reply
  • 3189 views

Hi Everyone,

 

I wanted to share a form I am attempting to get working. Here's the scoop:

I have 2 radio groups. Each radio has a Yes or No radio button.

When "Question1" radio button "Yes" is selected, the "Field1" text field activates. If "No" is selected, it deactivates "Field1" providing "Question2" radio button is "No".

When "Question2" radio button "Yes" is selected, all three text fields activate. If "No" is selected, it deactivates "Field2" and "Field3", and "Field1" deactivates providing "Question1" radio button is "No".

Basically, "Field1" applies to both radio groups, and can only deactivate if both radio groups have "No" selected.

 

I'm trying to determine if the JavaScript I have is correct or if I've made any mistakes I'm not aware of. Or, if there is a better/simpler way to do what I'm attempting. If you look at the attached file, you can see the JavaScript I have applied to the radio buttons in Properties > Actions.

 

For "Question1", I have this JavaScript on the "No" button:

 

this.getField("Field1").display = display.hidden;
this.getField("Field2").display = display.hidden;
this.getField("Field3").display = display.hidden;


if (this.getField("Question1").valueAsString=="No" && this.getField("Question2").valueAsString=="Yes") {
this.getField("Field1").display = display.visible;

} else {

this.getField("Field1").display = display.hidden;
}

 

I had to add  the 3 fields before the if "Question1" is set to "No" and "Question2" is set to "Yes" bit... I'm not sure what I did wrong but if I try putting this after it then it won't work. I'm guessing I have an error (maybe a few) as to why I couldn't get it working.

 

And lastly, I'm wondering if there is a way whenever "No" radio buttons are selected that the fields can clear so that if "Yes" is selected, any previous entries do not appear. I couldn't figure out how to do this, I tried adding resets to those fields on the "No" buttons, but then that would clear the entry even if "Yes" is selected in one of the groups. 

 

Any help here is greatly appreciated it. Thanks in advance.

This topic has been closed for replies.
Correct answer Nesa Nurani

Clearly still need coffee. Attached.


See if this works for you:

https://drive.google.com/uc?export=download&id=1twn8NAnDb-YAPFxhB5YmUbRrA6CKpcJi 

1 reply

Nesa Nurani
Community Expert
Community Expert
August 17, 2022

You said "Question2" show "Field1","Field2","Field3" and "Question1" show "Field1", in your file
you set it the other way, so I'm not sure which way you want?

Script for button:
Question1 "Yes":
if (this.getField("Question1").valueAsString=="Yes")
this.getField("Field1").display = display.visible;

Question1 "No":
if(this.getField("Question1").valueAsString=="No" && this.getField("Question2").valueAsString=="No"){
this.getField("Field1").display = display.hidden;
this.getField("Field1").value = "";}

Question2 "Yes":
if (this.getField("Question2").valueAsString=="Yes") {
this.getField("Field1").display = display.visible;
this.getField("Field2").display = display.visible;
this.getField("Field3").display = display.visible;}

Question2 "No":
if(this.getField("Question2").valueAsString=="No"){
this.getField("Field2").display = display.hidden;
this.getField("Field3").display = display.hidden;
this.getField("Field2").value = "";
this.getField("Field3").value = "";}

if(this.getField("Question1").valueAsString=="No" && this.getField("Question2").valueAsString=="No") {
this.getField("Field1").display = display.hidden;
this.getField("Field1").value = "";}

rform82Author
Known Participant
August 17, 2022

Thank you @Nesa Nurani and my mistake on how I worded my original post.

Question 1 "Yes" would show "Field1", "Field2", and "Field3".
Question 2 "Yes" would show "Field1".

For "Field1" to deactivate, "Question1" and "Question2" cannot be set to "Yes".


In the updated attachment, I currently need to select "No" for both radio groups in order for "Field1" to deactivate (this is even if I never chose "Yes" for both of the radio button groups first). So, I can choose "Yes" for "Question1" to activate "Field1", but need to choose "No" for both "Question1" and "Question2" (which wasn't selected) in order for "Field1" to deactivate. Is it possible to adjust this so that as long as "Yes" isn't selected for either radio button group then "Field1" will deactivate?

 

rform82Author
Known Participant
August 17, 2022

@Nesa Nurani updated the text to match the fields the radio buttons are meant for as I had them opposite of what I described. So, the question is whether or not "Field1" can be hidden as long as "Yes" isn't selected for either radio button group. Right now, I need to choose "No" for both radio groups, even if I only selected "Yes" for one of the groups.

 

Thanks