44

I am using Twitter's Bootstrap modal.

The modal is dismissed whenever the user clicks anywhere else on the screen except the modal.

Is there a way I can prevent this, so the user has to click the Close button to dismiss the modal?

Best,

user1725145
  • 3,927
  • 2
  • 36
  • 55
jackhao
  • 3,257
  • 3
  • 20
  • 41
  • 1
    Check the docs under "options": http://twitter.github.com/bootstrap/javascript.html#modals – Wesley Murch Nov 10 '12 at 07:01
  • Does this answer your question? [Disallow Twitter Bootstrap modal window from closing](https://stackoverflow.com/questions/9894339/disallow-twitter-bootstrap-modal-window-from-closing) – Sajjad Sep 29 '20 at 10:35

3 Answers3

64

In most cases, the modal dialog is constructed not using Javascript, rather it's using the markup, in this scenario, just add attribute: data-backdrop="static" in the div that has class="modal fade".

Example:

  <div class="modal fade" id="myModal" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
Will
  • 1,949
  • 20
  • 19
54

you can pass these options:

{
  keyboard: false,
  backdrop: 'static'
}
rahul
  • 7,348
  • 7
  • 36
  • 49
6

In some cases, this alternative solution may be useful.

$('#modalElement').on('hide.bs.modal', function () {
    return false;
});
Goose
  • 4,548
  • 4
  • 40
  • 82