How to close a modal only by buttons?

Asked

Viewed 5,641 times

2

The title is already very explanatory. But I would like to know how I close a modal only by the close button and the "x" of the window, not allowing the user to click on the black background around the pop up window to close.

inserir a descrição da imagem aqui

HTML:

<!-- 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">
  <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>

http://jsfiddle.net/1aeur58f/1/

1 answer

3


Use the option backdrop with data-backdrop="static". As set out in official documentation, this option makes the window modal do not close with the click, only with the buttons.

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

Example: http://jsfiddle.net/1aeur58f/99/

  • 1

    Well that’s right thanks man!

  • 2

    Also add the property data-keyboard="false" to avoid being closed with the keyboard.

  • Truth I just checked this problem kkkk thanks too Pedro!

  • 1

    As for documentation, as Maurivan said, it is always worth reading: http://getbootstrap.com/javascript/#modals-options

  • Good Luck Pedro!

Browser other questions tagged

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