Skip to main content
Participant
August 28, 2023
Question

Javascript for radio button to activate or deactivate text fields

  • August 28, 2023
  • 1 reply
  • 1244 views

I have a form with two connected radio buttons i'll like to connect each to specific text fields such that when one radio button is selected, it activates/deactivates specific text fields. The code i'm currently using does not work well during testing. I noticed that when I select one button, it works but when I change my mind and select the other button instead, it deactivates all the fields for both buttons. I used my clear form button to reset the form but the text fields remain deactivated even though no radio button is selected.

 

Here's the code I used and just interchanged the value and text fields for the substitute button:

 

if(this.getField('Shelter cost').value == 'renter')

{this.getField('mortgage frequency').readonly = true;

this.getField('monthly mortgage').readonly = true;

this.getField('biweekly mortgage').readonly = true;

this.getField('annual mortgage').readonly = true;

this.getField('annual mortgage 2').readonly = true;

this.getField('Property tax').readonly = true;}

else

{this.getField('mortgage frequency').readonly = false;

this.getField('mortgage frequency').value = "";

this.getField('monthly mortgage').readonly = false;

this.getField('monthly mortgage').value = "";

this.getField('biweekly mortgage').readonly = false;

this.getField('biweekly mortgage').value = "";

this.getField('annual mortgage').readonly = false;

this.getField('annual mortgage').value = "";

this.getField('annual mortgage 2').readonly = false;

this.getField('annual mortgage 2').value = "";

this.getField('Property tax').readonly = false;

this.getField('Property tax').value = "";}

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
August 28, 2023

Where did you place the code?

Do both script affect the same fields? If so, they will conflict with one another. You will need to create a single script that covers all options.

Participant
August 28, 2023

I placed the code in the Actions panel of the radio button with the trigger "Mouse Up" and action "Run a Javascript". I'm new to coding so it's all still kinda confusing for me. I want users to choose between being a renter or a homeowner. If they select the renter radio button, the text fields for mortgage information will be deactivated and they'll only be able to complete the fields for rent information but if they select the button for homeowner, they'll only be able to complete the text fields for mortgage information. What's the best way to go about it?

try67
Community Expert
Community Expert
August 29, 2023

OK, the issue is that the Clear Form command does not trigger this code, then.

You'll need to add a separate script to your Reset Form button to reset the readonly property of the fields to its default state.