javascriptjquerytwitter-bootstraptwitter-bootstrap-3

Understanding how data-dismiss attribute works in Bootstrap


I'm facing a problem with this example:

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

As per my understanding the data-dismiss="modal" attribute should close the modal if you click on it, but I don't understand how it works behind the scene. I checked the official documentation at: http://getbootstrap.com/javascript/#modals-examples but there's no explanation.


Solution

  • The hiding functionality is implemented in the modal.js in this way.

    this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
    

    Basically it's just finding the elements that have the attribute of data-dismiss and the value of modal. Upon click it will hide these elements.