Open modal second only after closing the first

3

It seems simple but with difficulties, 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 is the best solution for this? I've already tried .on('click') in the #ModalScore (primeiro modal) element but in it it has a slide where I click inside it to change the slide and ends up making the other modal appear.

    
asked by anonymous 18.01.2017 / 18:22

2 answers

3

Next you can use the event function of bootstrap itself:

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

I've removed other info if you need source

    
18.01.2017 / 18:32
3

Use hidden.bs.modal , that is, $('#myModal').on('hidden.bs.modal', function (e) { }); is the event that means closing 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><scriptsrc="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

18.01.2017 / 18:32