I have a modal with a long content and a button that opens another modal. When I close the inner modal, the outer modal is stuck. It cannot be scrolled, rather the background begins to scroll.
I have found a few questions relating to modal and scrolling like bootstrap modal inside a modal and Prevent BODY from scrolling when a modal is opened, but my issue doesn’t seem to be replicated here.
I have forked a fiddle from Bootstrap 3: prevent modal inside modal to trigger (hidden.bs.modal) every time to demonstate this issue. After clicking on the Cancel from the second modal, the first modal is stuck.
code:
$('#btnUploadCancel').click(function() {
$('#uploadImage').modal('toggle');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Open demo modal</button>
<p>
Lorem ipsum .... long content
</p>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Demo Modal</h4>
</div>
<div class="modal-body">
<p>
<a href="#" data-toggle="modal" data-target="#uploadImage" class="btn btn-primary">Upload image</a>
</p>
Lorem ipsum ... long content
<p>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
<div class="modal" id="uploadImage" tabindex="-1" role="dialog" aria-labelledby="uploadImage-title" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="uploadImage-title">Upload new image</h4>
</div>
<div class="modal-body">
Testing Area
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="btnUploadCancel">Cancel</button>
<button type="button" class="btn btn-success">Accept</button>
</div>
</div>
</div>
</div>
</div>
3
Answers
Edit your javascript to leave first modal open like this –
Bootstrap modal when shown adds a new
modal-open
class to yourbody
element. And when being closed, it removes themodal-open
class.So you only need to re-apply that class to your
body
element when closing the child modal. Here’s the thing:Add a new custom css class for the modals inside your parent modal.
in my case, I use
.child-modal
,html
See working snippet below: