Skip to content

Modal hidden event #16604

Closed
Closed

Description

@ray73864

Not sure what I'm doing wrong here, have managed to turn my issue into a simple example of what is going wrong at:

http://www.bootply.com/BzXT9pgtO7

Basically I'm trying to get it so that when a modal closes the hidden.bs.modal event sends the focus back to an input box on the page. I'm sure I've had this working before but cannot work out where I have gone wrong.

If I tell the hidden event to instead just put a value into the input box it shows up, I just can't get it to focus, I'm thinking it's a timing issue but then that's why I'm using the 'hidden' event as opposed to the 'hide' event.

Activity

  1. twbs-lmvtfy commented on Jun 9, 2015

    @twbs-lmvtfy

    Hi @ray73864!

    You appear to have posted a live example (http://s.bootply.com/render/BzXT9pgtO7), which is always a good first step. However, according to Bootlint, your example has some Bootstrap usage errors, which might potentially be causing your issue:

    • line 9, column 9: W013: Bootstrap version might be outdated. Latest version is at least 3.3.4 ; saw what appears to be usage of Bootstrap 3.3.1
    • line 71, column 9: W013: Bootstrap version might be outdated. Latest version is at least 3.3.4 ; saw what appears to be usage of Bootstrap 3.3.1

    You'll need to fix these errors and post a revised example before we can proceed further.
    Thanks!

    (Please note that this is a fully automated comment.)

  2. ray73864 commented on Jun 9, 2015

    @ray73864
    Author

    Revised example using Bootstrap 3.3.4 same issue:

    http://www.bootply.com/c7GmgLZx5H

  3. kkirsche commented on Jun 9, 2015

    @kkirsche
    Contributor

    Wrapping it in a setTimeout with a time of 1 ms fixes it.

    setTimeout(function() {
        $("#txtInput").focus();
    }, 1);

    See: http://www.bootply.com/hsCP97LMhZ

    EDIT: apparently this also works with a time of 0. It seems when you apply setTimeout with a delay of 0, you can be sure that the code you put in the delayed function is executed after the main processing is finished. See http://stackoverflow.com/questions/7046012/jquery-focus-on-input-field which discusses this

  4. ray73864 commented on Jun 9, 2015

    @ray73864
    Author

    Hrmm, kind of wanted to avoid using a setTimeout, but if that is the only way then so be it.

  5. kkirsche commented on Jun 9, 2015

    @kkirsche
    Contributor

    Understandable. I'm not sure it's the only way, but it seems to work to fix the issue even if you set a 0ms delay. For that reason you shouldn't actually slow down the application in any noticeable manner because you aren't actually delaying it.

  6. added
    jsJavaScript or TypeScript sources and plugins
    on Jun 9, 2015
  7. cvrebert commented on Jun 9, 2015

    @cvrebert
    Collaborator

    For accessibility reasons, after hiding a modal, we refocus the button that was used to trigger the showing of the modal (see

    $this.is(':visible') && $this.trigger('focus')
    ).
    I will make a note that we should try to better accommodate cases like yours in Bootstrap v4. For v3, short of modifying modal.js, I think only somewhat-hacky solutions are available.

  8. witheej commented on Jun 7, 2019

    @witheej

    The setTimeout workaround unfortunately does not solve the problem of IE throwing an error when it tries to refocus on an SVG that was clicked to trigger the modal (I'm using IE 11).

    See: focus-trap/focus-trap-react#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript or TypeScript sources and plugins

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions