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.
Bootstrap 4
See this JSFiddle for a working example:
http://jsfiddle.net/aq9Laaew/120440/
See the Modal Events section of the docs here:
https://getbootstrap.com/docs/4.1/components/modal/#events
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.
Bootstrap uses the
in
class when the modal is open. It is very important to use thehidden
event since the classin
is still defined when the eventhide
is triggered.This solution will not work in IE8 since IE8 does not support the Jquery
:not()
selector.I'm jumping in super late here, but for people using Bootstrap modals with React I've been using MutationObserver to detect changes in a modal's visibility and adjusting the state accordingly - this method could be applied to run any function when the modal is closed:
For those wondering about modern browser support, CanIUse has MutationObserver's coverage at around 87%
Hope that helps someone :)
Cheers,
Jake
I was having the same issues as some with
You need to place this at the bottom of the page, placing it at the top never fires the event.
I would do it like this:
The rest has already been written by others. I also recommend reading the documentation:jquery - on method
Bootstrap 3 & 4
Bootstrap 3: getbootstrap.com/javascript/#modals-events
Bootstrap 4: getbootstrap.com/docs/4.1/components/modal/#events
Bootstrap 2.3.2
See getbootstrap.com/2.3.2/javascript.html#modals → Events