Bootstrap Modal on Modal by JS

2

Good afternoon,

I saw several examples on the internet of a modal opening on another modal with the bootstrap, but none of them using javascript. Can not open a modal over the other with the bootstrap using javascript?

    
asked by anonymous 13.07.2015 / 20:06

4 answers

1

I've been through the same problem, you should keep information on how many modals are open, if a new one is opened it should have a z-index greater than what is already, to always appear in front, for this the events shows and hidden modal are treated. follows the code example:

$('.modal').on('hidden.bs.modal', function( event ) {
            $(this).removeClass( 'fv-modal-stack' );
            $('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) - 1 );
            });


$( '.modal' ).on( 'shown.bs.modal', function ( event ) {

               // keep track of the number of open modals

               if ( typeof( $('body').data( 'fv_open_modals' ) ) == 'undefined' )
               {
                 $('body').data( 'fv_open_modals', 0 );
               }


               // if the z-index of this modal has been set, ignore.

            if ( $(this).hasClass( 'fv-modal-stack' ) )
                    {
                    return;
                    }

            $(this).addClass( 'fv-modal-stack' );

            $('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) + 1 );

            $(this).css('z-index', 1040 + (10 * $('body').data( 'fv_open_modals' )));

            $( '.modal-backdrop' ).not( '.fv-modal-stack' )
                    .css( 'z-index', 1039 + (10 * $('body').data( 'fv_open_modals' )));


            $( '.modal-backdrop' ).not( 'fv-modal-stack' )
                    .addClass( 'fv-modal-stack' ); 

             });


    });

Sample site running: link

    
16.07.2015 / 16:38
1

Let's go straight to the answer :

Using setTimeout , because .modal-backdrop is not created when the show.bs.modal event is triggered.

$(document).ready(function () {
    $('#abrir').click(function () {
        $('#myModal').modal({
            show: true
        })
    });
        $(document).on('show.bs.modal', '.modal', function (event) {
            var zIndex = 1040 + (10 * $('.modal:visible').length);
            $(this).css('z-index', zIndex);
            setTimeout(function() {
                $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
            }, 0);
        });
});
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script><adata-toggle="modal" href="#myModal" class="btn btn-primary">Abrir</a>
<div class="modal fade" 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 1</h4>
            </div>
            <div class="container"></div>
            <div class="modal-body">Alguma coisa aqui dentro.
              <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Abrir</a>
            </div>
            <div class="modal-footer">
              <a href="#" data-dismiss="modal" class="btn btn-danger">Fechar</a>
	<a href="#" class="btn btn-primary">Outro botão a ser implementado</a>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal2">
    <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 2</h4>
            </div>
            <div class="container"></div>
            <div class="modal-body">Alguma coisa aqui dentro.
              <a data-toggle="modal" href="#myModal3" class="btn btn-primary">Abrir</a>
            </div>
            <div class="modal-footer">
              <a href="#" data-dismiss="modal" class="btn btn-danger">Fechar</a>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal3">
    <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 3</h4>
            </div>
            <div class="container"></div>
            <div class="modal-body">Acho que até aqui deu pra compreender...
            </div>
            <div class="modal-footer">
              <a href="#" data-dismiss="modal" class="btn btn-danger">Fechar</a>
            </div>
        </div>
    </div>
</div>

Reference

    
08.12.2015 / 20:31
0

Yes, you can open one modal over another using JavaScript. Here's an example:

link

HTML:

<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>

JavaScript:

$('#openBtn').click(function(){
    $('#myModal').modal({show:true})
});
    
13.07.2015 / 20:10
0

It is possible to use without javascript, just put a button inside an open modal to call the other modal.

If you want to use javascript to call Modal, use the following code:

$('.btn.chamar-modal-a').on('click',function(){
    $("#modalA").modal('show');
}

OBS : Pay attention to the Modal overlay in the code, place Modal A after Modal B, that is, Modal A on Modal B.

    
16.07.2015 / 15:51