Working on a project that has a lot of modals calling other modals and a few HTML guys that might not know to initiate it everytime for each button. Came to a similar conclusion as @gmaggio, begrudgingly after going the long way around first.
EDIT: Now supports modals called via javascript.
EDIT: Opening a scrolling modal from another modal now works.
$(document).on('show.bs.modal', function (event) {
if (!event.relatedTarget) {
$('.modal').not(event.target).modal('hide');
};
if ($(event.relatedTarget).parents('.modal').length > 0) {
$(event.relatedTarget).parents('.modal').modal('hide');
};
});
$(document).on('shown.bs.modal', function (event) {
if ($('body').hasClass('modal-open') == false) {
$('body').addClass('modal-open');
};
});
Just place the modal calling button in as normal, and if it is picked up to be inside a modal, it will close the current one first before opening the one specified in data-target.
Note that relatedTarget
is provided by Bootstrap.
I also added the following to smooth out the fading a bit: I am sure more can be done though.
.modal-backdrop.fade + .modal-backdrop.fade {
transition: opacity 0.40s linear 0s;
}