Gravity Forms: Prevent Scrolling on Next / Previous Ajax Submits

Step 1: Identify your Form ID

Figure out what your form ID is – look in the address bar as you edit your form:

Gravity Forms: Prevent Scrolling on Next / Previous Ajax Submits

Step 2: Add a Filter

Edit something like your theme’s functions.php file and add a filter for the form number:

add_filter('gform_confirmation_anchor_5','theme_gform_confirmation_anchor_5');

function theme_gform_confirmation_anchor_5($enabled)
{
    return false;
}

Step 3: Done

Now the code in the gravity forms plugin won’t fire that causes the form to scroll on ajax submits of multi-paged forms. (no need to change this, code for reference only):

if($use_anchor !== false) {
    $scroll_position['default'] = is_numeric($use_anchor) ? "jQuery(document).scrollTop(" . intval($use_anchor) . ");" : "jQuery(document).scrollTop(jQuery('#gform_wrapper_{$form_id}').offset().top);";
    $scroll_position['confirmation'] = is_numeric($use_anchor) ? "jQuery(document).scrollTop(" . intval($use_anchor) . ");" : "jQuery(document).scrollTop(jQuery('#gforms_confirmation_message').offset().top);";
}

Step 4: Replace scrolling behavior with something like jQuery Smooth Scroll

Use code similar to this to scroll to a certain element after one of the submit buttons has been clicked:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-smooth-scroll/1.4.12/jquery.smooth-scroll.min.js"></script>

<script>

jQuery(document).on('click','.gform_page_footer input', function() {


    jQuery.smoothScroll({
      scrollTarget: '.gform_wrapper',
      offset: -20

    });


 });

</script>

Related Posts:

  • No Related Posts
This entry was posted in Tech Tips and tagged , . Bookmark the permalink.

One Response to Gravity Forms: Prevent Scrolling on Next / Previous Ajax Submits

  1. Joel McDonald says:

    This worked beautifully. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *