How to check if bootstrap modal is open, so I can use jquery validate?

JqueryValidationTwitter BootstrapModal Dialog

Jquery Problem Overview


I need to make a validation only if a modal is open, because if I open it, and then I close it, and the I press the button that opens the modal it doesn't work because it is making the jquery validation, but not showing because the modal was dismissed.

So I want to ad a jquery if modal is open so the i do validate, is this possible?

<script>
$(document).ready(function(){

var validator =$('#form1').validate(
 {
  ignore: "",
  rules: {

usu_login: {
  required: true
},
usu_password: {
  required: true
},
usu_email: {
  required: true
},
usu_nombre1: {
  required: true
},
usu_apellido1: {
  required: true
},
usu_fecha_nac: {
  required: true
},
usu_cedula: {
  required: true
},
usu_telefono1: {
  required: true
},
rol_id: {
  required: true
},
dependencia_id: {
  required: true
},
  },

  highlight: function(element) {
$(element).closest('.grupo').addClass('has-error');
		if($(".tab-content").find("div.tab-pane.active:has(div.has-error)").length == 0)
		{
			$(".tab-content").find("div.tab-pane:hidden:has(div.has-error)").each(function(index, tab)
			{
				var id = $(tab).attr("id");
		
				$('a[href="#' + id + '"]').tab('show');
			});
		}
  },
  unhighlight: function(element) {
$(element).closest('.grupo').removeClass('has-error');
  }
 });

}); // end document.ready

</script>

Jquery Solutions


Solution 1 - Jquery

To avoid the race condition @GregPettit mentions, one can use:

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

// or, with the optional chaining operator (?.)
$("element").data('bs.modal')?._isShown    // Bootstrap 4
$("element").data('bs.modal')?.isShown     // Bootstrap <= 3

as discussed in Twitter Bootstrap Modal - IsShown.

When the modal is not yet opened, .data('bs.modal') returns undefined, hence the || {} - which will make isShown the (falsy) value undefined. If you're into strictness one could do ($("element").data('bs.modal') || {isShown: false}).isShown

Solution 2 - Jquery

You can use

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

Bootstrap adds the in class when the modal is open and removes it when closed

Solution 3 - Jquery

You can also directly use jQuery.

$('#myModal').is(':visible');

Solution 4 - Jquery

Bootstrap 2 , 3 Check is any modal open in page :

if($('.modal.in').length)

compatible version Bootstrap 2 , 3 , 4+

if($('.modal.in, .modal.show').length)

Only Bootstrap 4+

if($('.modal.show').length)

Solution 5 - Jquery

Check if a modal is open

$('.modal:visible').length && $('body').hasClass('modal-open')

To attach an event listener

$(document).on('show.bs.modal', '.modal', function () {
    // run your validation... ( or shown.bs.modal )
});

Solution 6 - Jquery

You can also use

$('#myModal').hasClass('show');

Solution 7 - Jquery

$("element").data('bs.modal').isShown

won't work if the modal hasn't been shown before. You will need to add an extra condition:

$("element").data('bs.modal')

so the answer taking into account first appearance:

if ($("element").data('bs.modal') && $("element").data('bs.modal').isShown){
... 
}

Solution 8 - Jquery

Why complicate things when it can be done with simple jQuery like following.

$('#myModal').on('shown.bs.modal', function (e) {
    console.log('myModal is shown');
    // Your actual function here
})

Solution 9 - Jquery

On bootstrap-modal.js v2.2.0:

( $('element').data('modal') || {}).isShown

Solution 10 - Jquery

As a workaround I personally use a custom global flag to determine whether the modal has been opened or not and I reset it on 'hidden.bs.modal'

Solution 11 - Jquery

JavaScript Approach to check for any particular model state by its ID.

modalstate = document.getElementById('modal-id').classList.contains('show')

This will returns true if particular modal-id is open.

Solution 12 - Jquery

this code indication, when modal is open(in body), or close (result false)

var trueFalse = ($('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
QuestionMariana HernandezView Question on Stackoverflow
Solution 1 - JqueryBrian M. HuntView Answer on Stackoverflow
Solution 2 - JquerytleefView Answer on Stackoverflow
Solution 3 - Jqueryalexoviedo999View Answer on Stackoverflow
Solution 4 - Jqueryuser5490177View Answer on Stackoverflow
Solution 5 - JqueryRaja KhouryView Answer on Stackoverflow
Solution 6 - JqueryBhavesh LalwaniView Answer on Stackoverflow
Solution 7 - JqueryjoanfihuView Answer on Stackoverflow
Solution 8 - JquerySokkaView Answer on Stackoverflow
Solution 9 - JqueryRicardoView Answer on Stackoverflow
Solution 10 - JqueryEdoardo VignatiView Answer on Stackoverflow
Solution 11 - JqueryYuvraj HingerView Answer on Stackoverflow
Solution 12 - JqueryNoe GarciaView Answer on Stackoverflow