Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap-modal.js requires tabindex="-1" for escape key to work in Chrome/FireFox? #4854

Closed
jdaigle opened this issue Aug 30, 2012 · 9 comments
Labels

Comments

@jdaigle
Copy link

jdaigle commented Aug 30, 2012

It appears that in order for the escape key to work with bootstrap-modal.js in Chrome and FireFox, your modal div must contain the attribute tabindex="-1". The example in the documentation includes this attribute but you can reproduce the issue by removing the attribute from the example modal.

I can reproduce this in Chrome 21 and FireFox 15. Interestingly this is not an issue in IE 8/9.

If this requirement is not a bug, then I think it should be noted in the documentation.

@mchiocca
Copy link

mchiocca commented Sep 5, 2012

Setting tabindex="-1" doesn't solve the problem on Opera 12.0.2. Modals still don't close when you hit the escape key.

@andrewdeandrade
Copy link

Hey @jdaigle,

Thanks for opening this issue! Unfortunately, it looks like it fails to pass the criteria neccessary for submitting to bootstrap. The following things are currently failing:

  • should include a jsfiddle/jsbin illustrating the problem if tagged with js but not a feature

For a full list of issue filing guidelines, please refer to the bootstrap issue filing guidelines.

thanks!

@jdaigle
Copy link
Author

jdaigle commented Sep 28, 2012

@malandrew Here is an example: http://jsbin.com/welcome/28240

Click the button and press escape, and nothing happens. If you edit the source and include tabindex="-1" on the #myModal div, suddenly it works (if you pop out the output).

@jbutz
Copy link

jbutz commented Jan 14, 2013

@mchiocca In regards to the opera issue it seems that the keyup events that are in the escape function are not triggered in Opera when the escape key is pressed. However, keypress events are triggered.

That said, it seems the keypress events are not triggered in Chrome, but the keyup events are.

@fat
Copy link
Member

fat commented Feb 8, 2013

yeah you need it.

@fat fat closed this as completed Feb 8, 2013
@yuhaoboris
Copy link

i meet a problem.
i put a CKEditor into the modal DIV, then i click the upload image button in editor, and i found the input element can't input anything.

@mikew
Copy link

mikew commented Nov 24, 2013

Same issue as @Borisyu, but with redactor. Removing tabindex="-1" solves it.

swwu pushed a commit to predictive-edge/jQuery-File-Upload that referenced this issue Feb 20, 2014
thaljef added a commit to Stratopan/metacpan-web that referenced this issue May 19, 2014
oiami pushed a commit to oiami/metacpan-web that referenced this issue Jun 9, 2014
oiami pushed a commit to oiami/metacpan-web that referenced this issue Jun 9, 2014
wjduenow pushed a commit to wjduenow/mld_gallery that referenced this issue Jan 18, 2015
@adigourdi
Copy link

I just happened to encounter this issue, the problem is related to focus being lost to the body

  • A sample modal with tabindex="-1" attribute, keeps the focus at the .modal element (document.activeElement returns the modal element)
  • When the tabindex="-1" is removed, the modal can't be focused to since the tabindex attribute is what makes it "focusable", therefore the esc key is not captured, and the body element handles the keydown

@Volmarg
Copy link

Volmarg commented May 21, 2020

I came here looking for solution where opening modal from within other modal is stealing focus when tabindex is -1, meanwhile i made my own solution to this problem as I use external package which uses tabindex.
Well i will just leave it here in case someone will have simillar case and no idea to fix that or make workaround:

export default(function(){
    window.bootbox_extension = {
        /**
         * When modal is being called from within bootbox it looses all the focus as bootbox has tabindex = -1
         */
        removeTabindexFromActiveModals: function(){
            let $allActiveBootboxesModals = $('.bootbox.modal');

            $allActiveBootboxesModals.each(function(index, modal){
               let $modal = $(modal);
               $modal.removeAttr('tabindex');
            });
        },
        /**
         * The removed tabindex should be restored otherwise it wont be closable by hitting ESC
         */
        restoreTabindexForActiveModals: function(){
            let $allActiveBootboxesModals = $('.bootbox.modal');

            $allActiveBootboxesModals.each(function(index, modal){
                let $modal = $(modal);
                $modal.attr('tabindex', '-1');
            });
        },
    }
}());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants