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