I have a page where one Bootstrap modal opens another modal.
The problem is that with each opened modal, it adds
Here is the working demo that I think will fit in your case.
$(".modal").on("shown.bs.modal", function () {
if ($(".modal-backdrop").length > 1) {
$(".modal-backdrop").not(':first').remove();
}
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal2" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal3">Open Modal 3</button>
</div>
</div>
</div>
</div>
</div>
// Make sure only one backdrop is rendered
$(document).on('show.bs.modal', '.modal', function () {
if ($(".modal-backdrop").length > 1) {
$(".modal-backdrop").not(':first').remove();
}
});
// Remove all backdrop on close
$(document).on('hide.bs.modal', '.modal', function () {
if ($(".modal-backdrop").length > 1) {
$(".modal-backdrop").remove();
}
});
$(document).on('show.bs.modal', '.modal', function () {
if ($(".modal-backdrop").length > -1) {
$(".modal-backdrop").not(':first').remove();
}
});
Let CSS handle it.
.modal-backdrop:nth-child(2n-1) {
opacity : 0;
}
https://jsfiddle.net/zsk4econ/3/
You can add/remove data-backdrop="false" attribute depends on requirement. Else you can also include css like
.modal-backdrop+.modal-backdrop {
opacity : 0;
}