javascriptjquerytwitter-bootstraptwitter-bootstrap-3modal-dialog

Multiple modals overlay


I need that the overlay shows above the first modal, not in the back.

Modal overlay behind

$('#openBtn').click(function(){
	$('#myModal').modal({show:true})
});
<a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a>

<div class="modal" id="myModal">
	<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Modal title</h4>
        </div><div class="container"></div>
        <div class="modal-body">
          Content for the dialog / modal goes here.
          <br>
          <br>
          <br>
          <br>
          <br>
          <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a>
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>
<div class="modal" id="myModal2" data-backdrop="static">
	<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Second Modal title</h4>
        </div><div class="container"></div>
        <div class="modal-body">
          Content for the dialog / modal goes here.
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>
    </div>
</div>


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>

I tried to change the z-index of .modal-backdrop, but it becomes a mess.

In some cases I have more than two modals on the same page.


Solution

  • Solution inspired by the answers of @YermoLamers & @Ketwaroo.

    Backdrop z-index fix
    This solution uses a setTimeout because the .modal-backdrop isn't created when the event show.bs.modal is triggered.

    $(document).on('show.bs.modal', '.modal', function() {
      const zIndex = 1040 + 10 * $('.modal:visible').length;
      $(this).css('z-index', zIndex);
      setTimeout(() => $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack'));
    });
    

    Example jsfiddle

    z-index
    If you don't like the hardcoded z-index for any reason you can calculate the highest z-index on the page like this:

    const zIndex = 10 +
      Math.max(...Array.from(document.querySelectorAll('*')).map((el) => +el.style.zIndex));
    

    Scrollbar fix
    If you have a modal on your page that exceeds the browser height, then you can't scroll in it when closing an second modal. To fix this add:

    $(document).on('hidden.bs.modal', '.modal',
      () => $('.modal:visible').length && $(document.body).addClass('modal-open'));
    

    Versions
    This solution is tested with bootstrap 3.1.0 - 3.3.5