Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save m1ndspark/ee7be01c1b7c5fbc61078be41ef0ef91 to your computer and use it in GitHub Desktop.

Select an option

Save m1ndspark/ee7be01c1b7c5fbc61078be41ef0ef91 to your computer and use it in GitHub Desktop.
Gravity Forms Validation Errors Mobile-friendly(er) - by Gravity Perks
<?php
/**
* Gravity Wiz // Gravity Forms // Give First Validation Error Focus
* http://gravitywiz.com/
*/
add_filter( 'gform_pre_render', 'gw_first_error_focus' );
function gw_first_error_focus( $form ) {
add_filter( 'gform_confirmation_anchor_' . $form['id'], '__return_false' );
?>
<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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment