37

I have such trouble: I have authentification which is made using Bootstrap modals.

When user opens sign in modal he can go to sign up modal ( or other ) . So, I need to close previous one.

Now I'm closing them like this:

$(document).ready(function(){
  $("a").click(function() {
  $("#login").hide();
  $("#sign_up").hide();
 })  
});

but when I'm doing this - I can't reopen sign in modal( after click on sign up from sign in) until I refresh the page.

Can someone suggest how to check on what modal I'm clicking on the link and close it, when call another one ? Is there any universal way to close previous modal ?

MID
  • 1,815
  • 4
  • 29
  • 40

3 Answers3

91

You hide Bootstrap modals with:

$('#modal').modal('hide');

Saying $().hide() makes the matched element invisible, but as far as the modal-related code is concerned, it's still there. See the Methods section in the Modals documentation.

Meeh
  • 2,538
  • 3
  • 18
  • 20
willglynn
  • 11,210
  • 48
  • 40
4

Toggle both modals

$('#modalOne').modal('toggle');
$('#modalTwo').modal('toggle');
C14L
  • 12,153
  • 4
  • 39
  • 52
Vikash Kumar
  • 41
  • 1
  • 5
1

The best that I've been able to do is

$(this).closest('.modal').modal('toggle');

This gets the modal holding the DOM object you triggered the event on (guessing you're clicking a button). Gets the closest parent '.modal' and toggles it. Obviously only works because it's inside the modal you clicked.

You can however do this:

$(".modal:visible").modal('toggle');

This gets the modal that is displaying (since you can only have one open at a time), and triggers the 'toggle' This would not work without ":visible"

TolMera
  • 452
  • 10
  • 25