Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jessedmatlock/dc40fe997b007a0d04ac7acb8ed16fc1 to your computer and use it in GitHub Desktop.
Save jessedmatlock/dc40fe997b007a0d04ac7acb8ed16fc1 to your computer and use it in GitHub Desktop.
WP Gravity Forms Scroll to Error
/**
* 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