Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3.0 Modal - How to dismiss with ESC or click outside modal?

I have a Bootply test setup here: http://bootply.com/67311

When I click the 'X' or the 'Close' button, it works as expected. However, I want it to close/dismiss when the user hits escape, or clicks outside the modal dialog.

How do I do this?

like image 530
Eric Avatar asked Jul 12 '13 13:07

Eric


People also ask

How do you close the modal by clicking outside of the modal box?

Modal Header You have two options to close this modal: Click on the "x" or click anywhere outside of the modal!

How do you stop modal close on outside click?

Data-keyword=”false” is to prevent closing modal while clicking Esc button, while data-backdrop=”static” , allows you keep modal pop-up opened when clicking on Gray area.

How do you modal dismiss?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.

How do you prevent closing the modal after click a button inside a modal?

You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true. In the following sample, the dialog is closed when you enter the username value with minimum 4 characters. Otherwise, it will not be closed.


1 Answers

Thanks a lot for "tabindex="-1"

Had same problem. I checked the code in bootstrap.js (Modal section) - looks like some event (whitch escape functionality uses) doesnt fire. I hope the fix it soon.

I got BS3 RC2, and mouse click works there perfectly

<!-- Button trigger modal -->
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">Launch demo modal</a>

<!-- Modal -->
<div class="modal" id="myModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <a href="#" class="btn">Close</a>
                <a href="#" class="btn btn-primary">Save changes</a>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
like image 182
Anton Avatar answered Oct 21 '22 10:10

Anton