Close Nested Form modal when ESC key is pressed or when clicked outside modal

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/
  2. Also add “Load Init Scripts Early” snippet to your theme’s functions.php. https://github.com/gravitywiz/snippet-library/blob/master/experimental/gfjs-early-init-scripts.php

Code

Filename: gpnf-close-modal-on-esc-or-outside-click.js

/**
 * Gravity Perks // Nested Forms // Close Nested Form modal when ESC key is pressed or when clicked outside modal
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 * 2. Also add "Load Init Scripts Early" snippet to your theme's functions.php.
 *    https://github.com/gravitywiz/snippet-library/blob/master/experimental/gfjs-early-init-scripts.php
 */
window.gform.addFilter( 'gpnf_modal_args', function( args, formId, fieldId, gpnf ) {
    // Only run for parent form ID 1. Remove this if the behavior is desired for all forms.
    if ( formId != 1 ) {
        return args;
    }

    args.closeMethods = ['overlay', 'button', 'escape'];

    return args;
} );

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.