45

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,993
  • 2
  • 37
  • 58
jackhao
  • 3,457
  • 3
  • 22
  • 43
  • 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

4 Answers4

65

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,989
  • 20
  • 19
54

you can pass these options:

{
  keyboard: false,
  backdrop: 'static'
}
rahul
  • 7,573
  • 7
  • 39
  • 53
7

In some cases, this alternative solution may be useful.

$('#modalElement').on('hide.bs.modal', function () {
    return false;
});
Goose
  • 4,764
  • 5
  • 45
  • 84
1

For new version of bootstrap at least 5.0 the correct way is:

<div class="modal fade" id="carDialog" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="carDialogLabel" aria-hidden="true">

Take a look at backdrop and keyboard it is: data-bs-backdrop and data-bs-keyboard.

Orions68
  • 21
  • 5