jQuery callback after Gravity Form submit
Asked Answered
C

2

6

Im trying to run some jQuery code after a failed form submit in Gravity forms aka when the validation has picked up something wrong.

I tried using the Ajax:complete callback but it doesnt fire at all.

The code i am trying to run basically adds listeners to a select dropdown, without it the calculations dont work which then makes the form unusable and unable to submit without refreshing the page.

Code that i am using below:

jQuery(document).ajaxComplete(function() {
    addServiceListeners();
}

function service_listeners() {

        var is_responsive = false;

        if(window_size < 1024 && $('body').hasClass('subNav-listener')) {
            is_responsive = true;
            $('.services-link').off('click');
            $('.services-link').on('click',function(e) {
                e.preventDefault();
                window.location = 'http://telviva.co.za/hosted-pbx';
            })
        } else {
            $('.services-link').off('click');
            $('#sub-nav-container').removeClass('hidden');
            $('.services-link').on('click',function(e) {
                if(window_size <= 600) {
                    if(e.target.hash == "#pbx-main") {
                        window.location = 'http://telviva.co.za/hosted-pbx';
                    } else {
                        return;
                    }
                } else {
                    e.preventDefault();
                $('#sub-nav-container').toggleClass('open');
                }
            }); 
        }

    }

All help appreciated!

Changeful answered 28/4, 2015 at 13:49 Comment(0)
P
23

You can call jQuery that has to be run after gravity form submit like this

<script type="text/javascript">
jQuery(document).bind('gform_post_render', function(){
   addServiceListeners();
});

Here is detail about 'gform_post_render' that is provided by Gravity form. https://www.gravityhelp.com/documentation/article/gform_post_render/

Portemonnaie answered 25/6, 2015 at 3:15 Comment(1)
Thanks for this, i did find it after some deep searching. Will mark it correct for future searches.Changeful
F
2

user2745337 has the correct answer! To elaborate on the solution, you should take your code and wrap it in a function. Call the function on your normal page load (or don't, up to you).

When the gform_post_render fires, you can then call your function (addServiceListeners())

===========

<script>
function addServiceListeners(){
   alert("Do AJAX DOM problem stuff here :)";
}
addServiceListeners(); // call it on page load? Go for it.

jQuery(document).bind('gform_post_render', function(){
   addServiceListeners();
});
</script>
Fula answered 16/2, 2016 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.