I use some jQuery on a current gravity form. However, when I submit the form and it comes back with bad validation, I lose some of the jQuery targets.
I’m curious how I can swap out $(document).ready(function() {
with something that will call my jQuery once the fields are reloaded with bad validation.
I’ve tried $("#gform_submit_button_1").click(function() {
however, that’s too soon. It needs to happen when the new fields come back from ajax.
There is actually a hook provided for use here:
gform_post_render
This jQuery hook is fired every time the form is rendered to allow custom jQuery to be executed. This includes initial form load, going to the next/previous page on multi-page forms, form rendered with validation errors, confirmation message displayed, etc.
http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/javascript/gform_post_render/
For some reason Gravity Forms still hasn’t added a jQuery hook for failed form validation. What they recommend doing is checking for the existence of
div.validation_error
.You’ll notice I’m not specifying a parent when I check for the validation error element:
jQuery('div.validation_error')
. If you have multiple forms on the page this would cause issues. Theform_id
parameter that is returned contains the form’s ID in the database (e.g. 1, 2, 35, etc.) but I’m not sure if this value matches the forms ID in the HTML, e.g.<form id="gform_1">
. If it does match, then it’s good practice to specify the parent, so you could do:Maybe somebody else could weigh in and let us know if the form’s HTML ID will always match the form’s database ID.
Gravity forms does supply a
gform_confirmation_loaded
hook, but I don’t think this will work in your case since it’s not loading the confirmation, but the error state form. They don’t have a hook for this but I’ve had success using jquery delegated events. I use the.gform_wrapper
as my first selector and then target the fields I want to actually target.See this documentation for more info:
One solution is: catch the submit event and start a interval that checks your form for changes and then calls your function:
This is however not very neat and it will only work if the html is actually changed after the Ajax call.