How to disable click out of modal?

2

I have a modal where I can view Google Maps using your particular API. 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>

When it is clicked out of modal, it disappears. I would like to disable this option so you can close it only by clicking the modal x button.

How to disable click out of modal ?

    
asked by anonymous 19.05.2017 / 23:06

1 answer

3

You can set property data-backdrop="static" 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" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="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>
    
19.05.2017 / 23:10