What You’ll Build
Gravity Forms conditional logic can show or hide fields. Sometimes you’d rather keep a field visible but disabled, so people can see the option exists but can’t pick it.
In this example, there are two radio button fields. Picking a certain choice in the first field disables the second field and clears its selection. It also works the other way around.
Requirements
- Gravity Forms (any license)
- Two Radio Buttons fields
- A place to add JavaScript, such as an HTML field on the form or a code snippet plugin like WPCodeBox
The Code
How to Customize It
The snippet uses this example setup. Change each value to match your form:
| In the code | What it means |
|---|---|
27 (in field_27_1) | Your form ID |
1 and 4 (in field_27_1, field_27_4) | The IDs of your two radio button fields |
'1' | The choice value in field 1 that disables field 4 |
'2' | The choice value in field 4 that disables field 1 |
The choice value is what’s saved, not the label. To see values, open the field’s choices and turn on Show Values.
Add It to Your Form
- Copy the code from the gist.
- Add an HTML field to your form and paste the code (including the
<script>tags) into its Content setting. Or add it as a JavaScript snippet that loads on the page with the form. - Update the form and field IDs and choice values.
- Preview the form and test both directions.
Things to Know
- Disabled fields aren’t submitted. If the disabled field is marked Required, the form won’t pass validation. Leave it optional, or use conditional logic to hide it instead.
- AJAX and multi-page forms: the code runs when the page loads. If your form reloads with AJAX or uses several pages, run it inside the
gform_post_renderevent so it runs again after each render. - Other field types: the same idea works for checkboxes. Change
input[type="radio"]toinput[type="checkbox"].
Need More Help?
If you still need help, use the chat or leave a comment below and we’ll get back to you.
Comments