How to disable click out of modal?

Asked

Viewed 5,183 times

5

I have a modal where 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>

By clicking anywhere other than on modal, it is closed. I would like to disable this option so that it can close only by clicking the button x modal.

How to disable click off modal?

1 answer

8


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

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

And if you wish 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>
<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>

  • 3

    Good answer Lucas, I modified it to avoid confusion about the understanding of backdrop and Keyboard ;)

  • 1

    Thank you @Guilhermenascimento

  • Simple and objective! Congratulations @Brtkca

Browser other questions tagged

You are not signed in. Login or sign up in order to post.