Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Created October 17, 2024 23:38
Show Gist options
  • Save chrisegg/02489abe087722ccba4c1fd95bd5c6dc to your computer and use it in GitHub Desktop.
Save chrisegg/02489abe087722ccba4c1fd95bd5c6dc to your computer and use it in GitHub Desktop.
This snippet will change a selection in a radio button field when entering a value to another field.
/**
* Gravity Ranger // Gravity Forms // Change radio button choice when adding value to another field
* https://gravityranger.com/gravity-forms-fancy-donation-form
*
* This snippet will change a selection in a radio button field when entering a value to another field.
* Intended for a donation form with a price select field and and a user defined price field.
*
*/
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Get the price field
var priceField = document.querySelector('#input_612_4'); // Price field: form ID 612, field ID 4
// Get the specific radio buttons (choices) by their individual IDs
var otherOption = document.querySelector('input#choice_612_14_4'); // Other choice (value other)
// Add event listener for when the price field is manually changed
priceField.addEventListener('input', function() {
// Automatically select the "Other" radio button when the user types into the price field
if (otherOption) {
otherOption.checked = true; // Select the "Other" option
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment