Can I check if Bootstrap Modal Shown / Hidden was?

Can I check if Bootstrap Modal is currently showing / hidden programmatically?

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

I need true / false

+60
javascript twitter-bootstrap twitter-bootstrap-3 bootstrap-modal
Oct 30 '13 at 5:42 on
source share
9 answers
 alert($('#myModal').hasClass('in')); 

It will return true if the modal is open.

+123
Oct 30 '13 at 5:46
source share

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 information see http://getbootstrap.com/javascript/#modals

+25
Oct 30 '13 at 7:42 on
source share

his old question, but anyway, something I used if someone was looking for the same

 if (!$('#myModal').is(':visible')) { // if modal is not shown/visible then do something } 
+20
Feb 11 '15 at 12:38
source share

When is modal hide? we check the following:

 $('.yourmodal').on('hidden.bs.modal', function () { // do something here }) 
+4
Jan 23 '15 at 5:51 on
source share

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

eg:

 if($('.modal').hasClass('in')){ //Do something here } 
+3
Aug 09 '17 at 10:25
source share

Officially:

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

{} used to avoid the case when the modal window is not yet open ( undefined returned). You can also set it to {isShown: false} to make it more {isShown: false} .

+3
Jan 15 '18 at 4:52
source share
 if($('.modal').hasClass('in')) { alert($('.modal .in').attr('id')); //ID of the opened modal } else { alert("No pop-up opened"); } 
+1
Aug 09 '17 at 9:48 on
source share

With Bootstrap 4:

 if ($('#myModal').hasClass('show')) { alert("Modal is visible") } 
+1
Mar 10 '19 at 15:30
source share

It works for me

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

0
May 25 '18 at 10:20
source share



All Articles