Close the first Bootstrap modal and open the new modal dynamically.
$('#Modal_One').modal('hide');
setTimeout(function () {
$('#Modal_New').modal({
backdrop: 'dynamic',
keyboard: true
});
}, 500);
try this:
$('.modal').on('hidden.bs.modal', function () {
//If there are any visible
if($(".modal:visible").length > 0) {
//Slap the class on it (wait a moment for things to settle)
setTimeout(function() {
$('body').addClass('modal-open');
},100)
}
});
The answer given by H Dog is great, but this approach was actually giving me some modal flicker in Internet Explorer 11. Bootstrap will first hide the modal removing the 'modal-open' class, and then (using H Dogs solution) we add the 'modal-open' class again. I suspect this is somehow causing the flicker I was seeing, maybe due to some slow HTML/CSS rendering.
Another solution is to prevent bootstrap in removing the 'modal-open' class from the body element in the first place. Using Bootstrap 3.3.7, this override of the internal hideModal function works perfectly for me.
$.fn.modal.Constructor.prototype.hideModal = function () {
var that = this
this.$element.hide()
this.backdrop(function () {
if ($(".modal:visible").length === 0) {
that.$body.removeClass('modal-open')
}
that.resetAdjustments()
that.resetScrollbar()
that.$element.trigger('hidden.bs.modal')
})
}
In this override, the 'modal-open' class is only removed when there are no visible modals on the screen. And you prevent one frame of removing and adding a class to the body element.
Just include the override after bootstrap have been loaded.
This thread is old, but for those who come from google, Ive come with a solutions that is hybrid from all the answers Ive found on the net.
This will make sure level class is being added:
$(document).on('show.bs.modal', '.modal', function (event) {
$(this).addClass(`modal-level-${$('.modal:visible').length}`);
});
Inside my SCSS Ive wrote a rule that supports main modal and 10 on top (10 because from z-index: 1060
popover takes place), you can add levels count inside _variables.scss
if you want:
@for $level from 0 through 10 {
.modal-level-#{$level} {
z-index: $zindex-modal + $level;
& + .modal-backdrop {
z-index: $zindex-modal + $level - 1;
}
}
}
Do not forget that you cannot have modal inside modal as their controls will be messed up. In my case all my modals were at the end of body
.
And finally as members below also mentions this, after closing one modal, you need to keep modal-open
class on body
:
$(document).on('hidden.bs.modal', function (e) {
if ($('.modal:visible').length > 0) {
$('body').addClass('modal-open');
}
});
Try this:
// Hide the login modal
$('#login').modal('hide');
// Show the next modal after the fade effect is finished
setTimeout(function(){ $('#lost').modal('show'); }, 500);
This simple hack works for me.
To open another modal window in a current opened modal window,
you can use bootstrap-modal
bootstrap-modal DEMO