javascriptoracle-apex

Problem with setting focus after displaying a modal


I have an Oracle Apex application that has an input field and some buttons.

The workflow is:

User enters text, presses enter. The screen changes to a different "page" that does not have the input field on. The user presses a button that posts a modal dialog. The user closes the dialog. The screen returns to the original screen with the input field.

The focus remains on the button that triggers the dialog. I would like to re-focus on the input field. I have tried a lot of variations on the following:

 apex.item( "XXXX" ).setFocus();
 setTimeout(function() { apex.item( "XXXX" ).setFocus(); console.log($(":focus"))}, 5000);
 setTimeout(function() { $( "XXXX" ).focus(); console.log($(":focus"))}, 5000);

None of these set the focus, it remains on the button. What is interesting is that if I click in the field to focus it manually, then click off so that it is no longer has focus, the setTimeout version of the code above will set the focus to the input field. So it seems like something is making it not "know" it can be focussed.

Has anyone seen anything like this behaviour? I don't think the fact that it's an Apex application is too relevant, it's in the javascript layer that the issue lies. I see the same behaviour running the code in the console.


Solution

  • The fix was putting a javascript snippet

    window.setTimeout(function() { document.getElementById('myInput').focus(); },100);
    

    Instead of using the Focus action. I guess the timing is complicated when there are a lot of dialogs in an application.