Created
March 15, 2024 22:02
-
-
Save joshuafredrickson/89d3899f4a9bcc9215a3eb19b5679b53 to your computer and use it in GitHub Desktop.
Gravity Forms: Perform an action while the form is submitting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Hook into the Gravity Forms submission process to show a loading state | |
*/ | |
const wrapper = document.querySelector( | |
'.gform_wrapper' | |
); | |
const gfSubmitting = `gf_submitting_${ | |
wrapper.querySelector('form').dataset.formid | |
}`; | |
let formSubmitting = false; // Private variable to hold the state | |
// Create computed properties for the "gf_submitting_{id}" property | |
Object.defineProperty(window, gfSubmitting, { | |
get() { | |
return formSubmitting; | |
}, | |
set(newValue) { | |
if (formSubmitting !== newValue) { | |
formSubmitting = newValue; | |
// Do something when the state changes | |
// Here, we're dialing down opacity while the AJAX spinner is spinning | |
wrapper.querySelector('.gform-body').style.opacity = newValue ? 0.3 : 1; | |
} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment