82

Can I check if Bootstrap Modal currently Shown / Hidden Programatically?

Like bool a = if("#myModal").shown(); ?

I need true/false

user2736812
  • 1,301
  • 4
  • 12
  • 15
  • possible duplicate of [jQuery if statement to check visibility](http://stackoverflow.com/questions/8616020/jquery-if-statement-to-check-visibility) – Phil Oct 30 '13 at 05:46

13 Answers13

146
alert($('#myModal').hasClass('in'));

It will return true if modal is open

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
32

The best method is given in the docs

$('#myModal').on('shown.bs.modal', function () {
  // will only come inside after the modal is shown
});

for more info refer http://getbootstrap.com/javascript/#modals

vipulsharma
  • 1,242
  • 1
  • 8
  • 16
  • Sure, that's the best method for executing a function when the modal appears, but that's different from checking if it's currently shown or hidden, as OP asked. My use-case, for example, is to perform an action on keypress, but only if a certain class of modal is visible. I don't see how this method can be used for that. – DHW Apr 05 '20 at 15:22
  • @DHW Couldn't you keep track of whether it's open or closed with those methods? If shown -> store that it's shown, when hidden -> store that it's hidden. Then you can just reference the relevant variables. – Anther May 19 '20 at 20:43
25

its an old question but anyway heres something i used incase someone was looking for the same thing

if (!$('#myModal').is(':visible')) {
    // if modal is not shown/visible then do something
}
ctf0
  • 6,991
  • 5
  • 37
  • 46
  • 1
    This just reads so nicely. Also not dependent on specific class selectors which could prove fragile. – Mike Rapadas May 07 '15 at 22:37
  • 5
    However, I believe it could return a false positive if it's executed during the showing/hiding animation. – Dinei Aug 19 '15 at 20:42
  • maybe but even though the function waits till the animation is finished to check if its visible or hidden as this one works the same as using normal css. – ctf0 Sep 17 '15 at 21:51
9

All Bootstrap versions:

var isShown = $('.modal').hasClass('in') || $('.modal').hasClass('show')

To just close it independent of state and version:

$('.modal button.close').click()

more info

Bootstrap 3 and before

var isShown = $('.modal').hasClass('in')

Bootstrap 4

var isShown = $('.modal').hasClass('show')
estani
  • 24,254
  • 2
  • 93
  • 76
7

When modal hide? we check like this :

$('.yourmodal').on('hidden.bs.modal', function () {
    // do something here
})
Kamlesh Kumar
  • 1,632
  • 2
  • 21
  • 31
3

Use hasClass('in'). It will return true if modal is in OPEN state.

E.g:

if($('.modal').hasClass('in')){
   //Do something here
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
3

In offical way:

> ($("element").data('bs.modal') || {})._isShown    // Bootstrap 4
> ($("element").data('bs.modal') || {}).isShown     // Bootstrap <= 3

{} is used to avoid the case that modal is not opened yet (it return undefined). You can also assign it equal {isShown: false} to keep it's more make sense.

Sakata Gintoki
  • 1,817
  • 16
  • 23
  • this is what I was after, it already returns `true` while the modal is opening, but before the `shown` class is applied – kiw Jan 26 '19 at 19:08
3

Here's some custom code that gives the modal states more explicitly named classes:

$('.modal').on('show.bs.modal', function(e)
{
    e.currentTarget.classList.add("modal-fading-in");
    e.currentTarget.classList.remove("modal-fading-out");
    e.currentTarget.classList.remove("modal-hidden");
    e.currentTarget.classList.remove("modal-visible");
});

$('.modal').on('hide.bs.modal', function(e)
{
    e.currentTarget.classList.add("modal-fading-out");
    e.currentTarget.classList.remove("modal-fading-in");
    e.currentTarget.classList.remove("modal-hidden");
    e.currentTarget.classList.remove("modal-visible");
});

$('.modal').on('hidden.bs.modal', function(e)
{
    e.currentTarget.classList.add("modal-hidden");
    e.currentTarget.classList.remove("modal-fading-in");
    e.currentTarget.classList.remove("modal-fading-out");
    e.currentTarget.classList.remove("modal-visible");
});

$('.modal').on('shown.bs.modal', function(e)
{
    e.currentTarget.classList.add("modal-visible");
    e.currentTarget.classList.remove("modal-fading-in");
    e.currentTarget.classList.remove("modal-fading-out");
    e.currentTarget.classList.remove("modal-hidden");
});

You can then easily target the modal's various states with both JS and CSS.

JS example:

if (document.getElementById('myModal').hasClass('modal-fading-in'))
{
    console.log("The modal is currently fading in. Please wait.");
}

CSS example:

.modal-fading-out, .modal-hidden
{
    opacity: 0.5;
}
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
2

With Bootstrap 4:

if ($('#myModal').hasClass('show')) {
    alert("Modal is visible")
}
NaturalBornCamper
  • 3,675
  • 5
  • 39
  • 58
1
if($('.modal').hasClass('in')) {
    alert($('.modal .in').attr('id')); //ID of the opened modal
} else {
    alert("No pop-up opened");
}
Jaykishan
  • 1,409
  • 1
  • 15
  • 26
1

For me this works

 
if($("#myModal").css("display") !='none' && $("#myModal").css("visibility") != 'hidden')

alert("modal shown");

mars328
  • 573
  • 2
  • 8
  • 26
0

I try like this with function then calling if needed a this function. Has been worked for me.

function modal_fix() {
var a = $(".modal"),
    b = $("body");
a.on("shown.bs.modal", function () {
    b.hasClass("modal-open") || b.addClass("modal-open");
});
}
Adhitya
  • 660
  • 1
  • 4
  • 18
-1

This resolved your problems, if true no refresh, and false refresh

var truFalse = $('body').hasClass('modal-open');
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Noe Garcia
  • 11
  • 2