603

I am using the Twitter Bootstrap lib on a new project and I want for part of the page to refresh and retrieve the latest json data on modal close. I dont see this anywhere in the documentation can someone point it out to me or suggest a solution.

Two problems with using the documented methods

 $('#my-modal').bind('hide', function () {
   // do something ...
 });

I attach a "hide" class to the modal already so it does not display on page load so that would load twice

even if I remove the hide class and set the element id to display:none and add console.log("THE MODAL CLOSED"); to the function above when I hit close nothing happens.

Liam
  • 27,717
  • 28
  • 128
  • 190
BillPull
  • 6,853
  • 15
  • 60
  • 99

12 Answers12

1256

Bootstrap 3 & 4

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

Bootstrap 3: getbootstrap.com/javascript/#modals-events

Bootstrap 4: getbootstrap.com/docs/4.1/components/modal/#events

Bootstrap 2.3.2

$('#myModal').on('hidden', function () {
    // do something…
});

See getbootstrap.com/2.3.2/javascript.html#modals → Events

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
Ricardo Lima
  • 12,576
  • 1
  • 13
  • 2
  • 3
    This seems to work except, for some reason, when hovering over a button in the modal footer it also fires. The button is a regular submit button: Any idea how to not have the hide event fire when hovering over it? Or, how I can detect that it was fired because of the hover? BTW: even though the event is fired the modal dialog doesn't close. – Guy Nov 04 '13 at 20:49
  • 3
    I'm not seeing this behaviour with Bootstrap 3, can you make a fiddle? – sp00n Dec 03 '13 at 23:23
  • 10
    This answer should probably mention the difference between hidden.bs.modal and hide.bs.modal. Hidden is fired when the CSS animations complete, but in my testing if there are no animations, it never fires (could just be a bug though). If you're working with data management, it could be safer to use hide.bs.modal instead, which is fired as soon as .modal('hide') is called. http://getbootstrap.com/javascript/#modals-usage - See Events subsection. – Evan Steinkerchner Aug 11 '14 at 16:15
  • 3
    Bootstrap 3: It works when I click on a modal tag marked as `data-dismiss="modal"` (like a close button), but **it does not work** when I click on the black background area surrounding the modal. The modal is dismissed but then it can't be reopened until the page is refreshed. I've tried both `'hidden.bs.modal'` and `'hide.bs.modal'` to no avail. – marquito Feb 19 '15 at 13:38
  • 6
    @marquito What you can do (what I do) is use it like this: `$('#myModal').modal({ backdrop: 'static', keyboard: false });` That way, the modal won't close when click on grey area, nor when pressing Esc key. – aesede Jun 25 '15 at 14:56
  • bootstrap 4 please – Toskan Sep 17 '18 at 19:36
  • Adding (or backwards adding in this case) to what @EvanSteinkerchner said: if you find yourself working with an old version of Bootstrap (I am working with 2.3.1) there too is a distinct difference between `hide` and `hidden`. They are the same as BS3. – davidhartman00 Jul 26 '19 at 16:45
  • using `$('#myModal').on("hide.bs.modal", function() {...}); ` for me works – FABBRj Feb 25 '20 at 11:40
  • @Guy did you ever figure out the solution? – doitlikejustin Oct 10 '20 at 00:19
  • @doitlikejustin nope – Guy Oct 11 '20 at 11:41
137

Bootstrap 4

$('#my-modal').on('hidden.bs.modal', function () {
  window.alert('hidden event fired!');
});

See this JSFiddle for a working example:

https://jsfiddle.net/6n7bg2c9/

See the Modal Events section of the docs here:

https://getbootstrap.com/docs/4.3/components/modal/#events

aar0n
  • 1,546
  • 1
  • 10
  • 10
54

Starting Bootstrap 3 (edit: still the same in Bootstrap 4) there are 2 instances in which you can fire up events, being:

1. When modal "hide" event starts

$('#myModal').on('hide.bs.modal', function () { 
    console.log('Fired at start of hide event!');
});  

2. When modal "hide" event finished

$('#myModal').on('hidden.bs.modal', function () {
    console.log('Fired when hide event has finished!');
});

Ref: http://getbootstrap.com/javascript/#js-events

aesede
  • 5,541
  • 2
  • 35
  • 33
22

In stead of "live" you need to use "on" event, but assign it to the document object:

Use:

$(document).on('hidden.bs.modal', '#Control_id', function (event) {
// code to run on closing
});
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Oscar
  • 697
  • 6
  • 8
15
$(document.body).on('hidden.bs.modal', function () {
    $('#myModal').removeData('bs.modal')
});
SUNny.K
  • 151
  • 1
  • 5
9

Boootstrap 5+ #ES6+

fewer dependencies -> Like other frameworks, Bootstrap will go back to standard thanks to new event methods

var myModalEl = document.getElementById('myModalID');
myModalEl.addEventListener('hidden.bs.modal', function (event) {
    // do something...
});

See this JSFiddle for a working example:

https://jsfiddle.net/Metamagikum/cw5f76bp/3/

The official documentation is located here:

https://getbootstrap.com/docs/5.0/components/modal/

Meep3D
  • 3,803
  • 4
  • 36
  • 55
metamagikum
  • 1,307
  • 15
  • 19
  • 2
    Thanks for being the only pure JS solution - I'm sick of the tiniest snippets of JavaScript assuming an entire jQuery dependency. – Hashim Aziz Jan 05 '22 at 00:33
6

Bootstrap provide events that you can hook into modal, like if you want to fire a event when the modal has finished being hidden from the user you can use hidden.bs.modal event like this

    /* hidden.bs.modal event example */
    $('#myModal').on('hidden.bs.modal', function () {
          window.alert('hidden event fired!');
    })

Check a working fiddle here read more about modal methods and events here in Documentation

Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
5

Bootstrap 4:

$('#myModal').on('hidden.bs.modal', function (e) {
   // call your method
})

hide.bs.modal: This event is fired immediately when the hide instance method has been called.

hidden.bs.modal: This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).

sendon1982
  • 9,982
  • 61
  • 44
4

I would do it like this:

$('body').on('hidden.bs.modal', '#myModal', function(){ //this call your method });

The rest has already been written by others. I also recommend reading the documentation:jquery - on method

tkartas
  • 51
  • 1
3

I've seen many answers regarding the bootstrap events such as hide.bs.modal which triggers when the modal closes.

There's a problem with those events: any popups in the modal (popovers, tooltips, etc) will trigger that event.

There is another way to catch the event when a modal closes.

$(document).on('hidden','#modal:not(.in)', function(){} );

Bootstrap uses the in class when the modal is open. It is very important to use the hidden event since the class in is still defined when the event hideis triggered.

This solution will not work in IE8 since IE8 does not support the Jquery :not() selector.

Tortus
  • 141
  • 7
3

if you want to fire a function on every modal close, you can use this method,

$(document).ready(function (){
    $('.modal').each(function (){
        $(this).on('hidden.bs.modal', function () {
            //fires when evey popup close. Ex. resetModal();
        });
    });
});

So you don't need to specify modal ids every time.

Disapamok
  • 1,426
  • 2
  • 21
  • 27
2

I was having the same issues as some with

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

You need to place this at the bottom of the page, placing it at the top never fires the event.

  • I would define the rules slightly tighter than that. When the jQuery selector `$("#myModal")` is invoked the HTML element with an ID of `myModal` needs to exist in the DOM. So you could have this code at the top of the page but placed within a `$(document).on("ready", function(){ ... });` (or similar function that is called after the DOM is populated with the element). – Andy Gee Apr 27 '20 at 03:54