Created
April 13, 2021 21:09
-
-
Save jessedmatlock/dc40fe997b007a0d04ac7acb8ed16fc1 to your computer and use it in GitHub Desktop.
WP Gravity Forms Scroll to Error
This file contains hidden or 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
/** | |
* Jump to the first error after submission | |
* | |
* @param $form | |
* @return mixed | |
*/ | |
function gf_scroll_to_first_error_focus( $form ) { | |
?> | |
<script type="text/javascript"> | |
if( window['jQuery'] ) { | |
( function( $ ) { | |
$( document ).bind( 'gform_post_render', function() { | |
var $firstError = $( 'li.gfield.gfield_error:first' ); | |
if( $firstError.length > 0 ) { | |
$firstError.find( 'input, select, textarea' ).eq( 0 ).focus(); | |
document.body.scrollTop = $firstError.offset().top; | |
} | |
} ); | |
} )( jQuery ); | |
} | |
</script> | |
<?php | |
return $form; | |
} | |
add_action( 'gform_pre_render', 'gf_scroll_to_first_error_focus', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment