Can I check if Bootstrap Modal Shown / Hidden?

JavascriptTwitter BootstrapTwitter Bootstrap-3Bootstrap Modal

Javascript Problem Overview


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

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

I need true/false

Javascript Solutions


Solution 1 - Javascript

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

It will return true if modal is open

Solution 2 - Javascript

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

Solution 3 - Javascript

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
}

Solution 4 - Javascript

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

Solution 5 - Javascript

When modal hide? we check like this :

$('.yourmodal').on('hidden.bs.modal', function () {
	// do something here
})

Solution 6 - Javascript

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

E.g:

if($('.modal').hasClass('in')){
   //Do something here
}

Solution 7 - Javascript

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.

Solution 8 - Javascript

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

Solution 9 - Javascript

With Bootstrap 4:

if ($('#myModal').hasClass('show')) {
    alert("Modal is visible")
}

Solution 10 - Javascript

if($('.modal').hasClass('in')) {
    alert($('.modal .in').attr('id')); //ID of the opened modal
} else {
    alert("No pop-up opened");
}

Solution 11 - Javascript

For me this works

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

alert("modal shown");

Solution 12 - Javascript

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");
});
}

Solution 13 - Javascript

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

var truFalse = $('body').hasClass('modal-open');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2736812View Question on Stackoverflow
Solution 1 - Javascriptuser2663434View Answer on Stackoverflow
Solution 2 - JavascriptvipulsharmaView Answer on Stackoverflow
Solution 3 - Javascriptctf0View Answer on Stackoverflow
Solution 4 - JavascriptestaniView Answer on Stackoverflow
Solution 5 - JavascriptKamlesh KumarView Answer on Stackoverflow
Solution 6 - JavascriptPEHLAJView Answer on Stackoverflow
Solution 7 - JavascriptSakata GintokiView Answer on Stackoverflow
Solution 8 - JavascriptPikamander2View Answer on Stackoverflow
Solution 9 - JavascriptNaturalBornCamperView Answer on Stackoverflow
Solution 10 - JavascriptJaykishanView Answer on Stackoverflow
Solution 11 - JavascriptMónica CifuentesView Answer on Stackoverflow
Solution 12 - JavascriptAdhityaView Answer on Stackoverflow
Solution 13 - JavascriptNoe GarciaView Answer on Stackoverflow