Skip to content
the poor coder

how to check with jquery if bootstrap modal is hidden or shown

Poor Man
Published
Reading time
1 min read
On this page

How to Check with jQuery if Bootstrap Modal is Hidden or Shown#

If you are using Bootstrap for your website's frontend development, you might encounter the need to check whether a modal is hidden or shown. It can be useful in cases where you need to perform some actions based on the state of the modal.

One way to achieve this is by using jQuery. Here are two methods to do so:

Method 1: Using .hasClass() method#

The .hasClass() method checks if the selected element has a specific class. In the case of modals, Bootstrap adds the 'show' class when it is visible and removes it when it is hidden. Therefore, we can use the .hasClass() method to check if the modal has the 'show' class.

Text
$('#myModal').hasClass('show'); // Returns true if the modal is shown

This code selects the modal with ID 'myModal' and checks if it has the 'show' class. If it returns true, then the modal is currently shown.

Method 2: Using .is() method#

The .is() method checks if the selected element matches a specific selector or condition. We can use this method to check if the modal is visible by selecting its backdrop element.

Text
$('.modal-backdrop').is(':visible'); // Returns true if the modal is shown

This code selects the modal's backdrop element and checks if it is visible. If it returns true, then the modal is currently shown.

Both methods are equally effective and depend on your preference. Choose the one that works best for your code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC