Open second modal only after closing the first

Asked

Viewed 1,785 times

3

It seems simple but I’m struggling, I have two modal that has to appear in the home after login of the user, I need to appear one and only after the user closes that first appears the second modal. I have the following code:

$(document).ready(function(){

  $("#ModalUpdateData").modal("hide");

  setTimeout(function(){
    if (!Cookies.get("ModalScore")){
      $("#ModalScore").modal("show");
      Cookies.set("ModalScore", true);
    } else {

    }
  },3000);
  
  setTimeout(function(){
    if (!Cookies.get("ModalUpdateData")){
      $("#ModalUpdateData").modal("show");
      Cookies.set("ModalUpdateData", true);
    } else {

    }
  },3000);
});

What’s the best solution for that? I’ve tried the .on('click')in the element #ModalScore (primeiro modal) but it has a slide where I click inside it to change slide and end up making the other modal appear.

  • You can start the second from the closing click of the 1st...

  • Which plugins are you using? Which modal?

  • @Magichat then but the first closes either by clicking on 'x' or outside the modal.

  • 1

    @Virgilionovic guy uses the same bootstrap modal.

  • what version of bootstrap ?

2 answers

2

Utilize hidden.bs.modal, that is to say, $('#myModal').on('hidden.bs.modal', function (e) { }); is the event that means the closing of the modal and in it calls the next modal with $("#myModal2").modal('show');

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
  $("#myModal2").modal('show');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/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"/>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title 1</h4>
      </div>
      <div class="modal-body">
        <p>MODAL 1</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title 2</h4>
      </div>
      <div class="modal-body">
        <p>MODAL 2</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

References:

  • 1

    Yours already comes with HTML +1...

  • 1

    Thanks man was that, had seen the @Magichat before and had got your answer is equal but already with HTML, thanks =D

  • No problem @Erick

2


Next you can use the event function itself bootstrap:

$('#myModal').on('hidden.bs.modal', function (e) {
  // abre o outro modal  aqui...
})

I pulled from here has other info if you need source

  • Genius exactly that =D simple and easy and I breaking my head.

  • Well, if any of the questions are correct, consider validating them by clicking on the green icon below the rating arrows (Bah!! 2 are so sure and are equal. hahah chooses one after hug)...

  • I chose it already, as yours was first and I had already chosen it. Thanks again.

  • +1 also the solutions were posted at the same time, but, what counts is to be objective and of course ... !!! vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.