10

I have a Bootply test setup here: http://bootply.com/67311

When I click the 'X' or the 'Close' button, it works as expected. However, I want it to close/dismiss when the user hits escape, or clicks outside the modal dialog.

How do I do this?

madth3
  • 7,275
  • 12
  • 50
  • 74
Eric
  • 669
  • 3
  • 10
  • 24

2 Answers2

6

Thanks a lot for "tabindex="-1"

Had same problem. I checked the code in bootstrap.js (Modal section) - looks like some event (whitch escape functionality uses) doesnt fire. I hope the fix it soon.

I got BS3 RC2, and mouse click works there perfectly

<!-- Button trigger modal -->
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">Launch demo modal</a>

<!-- Modal -->
<div class="modal" id="myModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <a href="#" class="btn">Close</a>
                <a href="#" class="btn btn-primary">Save changes</a>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Anton
  • 422
  • 4
  • 7
2

So I use this code and it works:

  <div class="modal hide fade" id="modalCreate">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h3>Create a new App</h3>
    </div>
    <div class="modal-body">
      Body stuff here

    <div class="modal-footer">
      <button type="submit" class="btn btn-primary">Save</button>
      <button class='btn btn-danger' data-dismiss='modal' aria-hidden='true'>Cancel</button>
    </div> 
  </div>

Also make sure you include the bootstrap.js in your file

punkdata
  • 885
  • 8
  • 15
  • Thanks - but this is giving me weird results. I suspect something about this is different in Bootstrap 3.0, because I see that in many examples for Bootstrap 2.x and it works there. Add it to the Bootply and see what I mean. – Eric Jul 12 '13 at 13:52
  • 1
    I added tabindex="-1" to get: – Eric Jul 12 '13 at 14:06
  • @Eric I have the same problem, check your bootstrap js version, after switching from 3.0.0 to 2.3.2, it works again. – Yuming Cao Oct 04 '14 at 23:05