26

I have code modal:

    <-- Button to trigger modal -->
<div id="result"></div>
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>

<-- Modal -->
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>

And i have code when should been after close moal window:

$('#result').html('yes,result');

Tell me please how to make that when closing the modal window(close or hide) executed a second code ?

madth3
  • 7,275
  • 12
  • 50
  • 74
Leo Loki
  • 2,457
  • 6
  • 24
  • 29

5 Answers5

63

If you're using version 3.x of Bootstrap, the correct way to do this now is:

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

Scroll down to the events section to learn more.

http://getbootstrap.com/javascript/#modals-usage

This appears to remain unchanged for whenever version 4 releases (http://v4-alpha.getbootstrap.com/components/modal/#events), but if it does I'll be sure to update this post with the relevant information.

MattD
  • 4,220
  • 2
  • 34
  • 44
32

I find answer. Thanks all but right answer next:

$("#myModal").on("hidden", function () {
  $('#result').html('yes,result');
});

Events here http://bootstrap-ru.com/javascript.php#modals

UPD

For Bootstrap 3.x need use hidden.bs.modal:

$("#myModal").on("hidden.bs.modal", function () {
  $('#result').html('yes,result');
});
Lokesh Sanapalli
  • 1,012
  • 3
  • 18
  • 39
Leo Loki
  • 2,457
  • 6
  • 24
  • 29
3

Few answers that may be useful, especially if you have dynamic content.

$('#dialogueForm').live("dialogclose", function() {
    //your code to run on dialog close
});

Or, when opening the modal, have a callback.

$( "#dialogueForm" ).dialog({
    autoOpen: false,
    height: "auto",
    width: "auto",
    modal: true,
    my: "center",
    at: "center",
    of: window,
    close : function() {
        // functionality goes here
    }
});
linktoahref
  • 7,812
  • 3
  • 29
  • 51
PodTech.io
  • 4,874
  • 41
  • 24
0
$('.close').click(function() {
  //Code to be executed when close is clicked
  $('#result').html('yes,result');
});
Huy
  • 10,806
  • 13
  • 55
  • 99
0

First check if modal div is proper,

<!-- Button trigger modal -->
<button type="button" 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>

Reference: https://jsfiddle.net/jakecigar/DTcHh/42400/

Walk
  • 1,531
  • 17
  • 21