Leo Loki
Leo Loki

Reputation: 2585

How to add an event after close the modal window?

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 ?

Upvotes: 27

Views: 146293

Answers (5)

Walk
Walk

Reputation: 1649

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/

Upvotes: 0

PodTech.io
PodTech.io

Reputation: 5264

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
    }
});

Upvotes: 3

Leo Loki
Leo Loki

Reputation: 2585

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');
});

Upvotes: 32

MattD
MattD

Reputation: 4420

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.

Upvotes: 63

Huy
Huy

Reputation: 11206

$('.close').click(function() {
  //Code to be executed when close is clicked
  $('#result').html('yes,result');
});

Upvotes: 0

Related Questions