bootstrap-3 – How to disable click outside modal?

Question:

I have a modal that I use to view the Google Maps Map. See the code below:

<div class="modal fade" id="myMapModal" >
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close">×</button>
                <h4 class="modal-title">Mapa</h4>
            </div>
            <div id="map-canvas" class="modal-lg"></div>
        </div>
    </div>
</div>

Clicking anywhere other than the modal closes it. I would like to disable this option so that it can close only when clicking the x button of the modal.

How to disable click outside modal ?

Answer:

You can set the data-backdrop="static" property to your modal:

<div class="modal hide" data-backdrop="static">

And if you want to disable ESC also use data-keyboard="false" :

<div class="modal hide" data-backdrop="static" data-keyboard="false">

Example:

jQuery(document).ready(function(e) {
    jQuery('#mymodal').trigger('click');
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!-- Button trigger modal -->
<button type="button" id="mymodal" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
Scroll to Top