diff --git a/js/formidable.js b/js/formidable.js index e60856dd76..fffcda4ef5 100644 --- a/js/formidable.js +++ b/js/formidable.js @@ -1335,7 +1335,7 @@ function frmFrontFormJS() { do { element = element.previousSibling; if ( -1 !== [ 'input', 'select', 'textarea' ].indexOf( element.nodeName.toLowerCase() ) ) { - element.focus(); + focusInput( element ); break; } @@ -1361,6 +1361,22 @@ function frmFrontFormJS() { } while ( element.previousSibling ); } + /** + * Focus a visible input, or possibly delay the focus event until the form has faded in. + * + * @since x.x + * + * @param {HTMLElement} input + * @return {void} + */ + function focusInput( input ) { + if ( input.offsetParent !== null ) { + input.focus(); + } else { + triggerCustomEvent( document, 'frmMaybeDelayFocus', { input }); + } + } + /** * Does the same as jQuery( document ).on( 'event', 'selector', handler ). *