How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Check if a Bootstrap Modal Is Open or Closed with jQuery
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Check if a Bootstrap Modal Is Open or Closed with jQuery
Web Development

How to Check if a Bootstrap Modal Is Open or Closed with jQuery

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Check Bootstrap modal open or closed with jQuery
SHARE

To check if a Bootstrap modal is open or closed with jQuery, use $('#myModal').is(':visible') — returns true when the modal is showing, false when hidden. For event-driven logic (running code when the modal opens), use the show.bs.modal and hidden.bs.modal events instead.

Contents
  • TL;DR
  • Snapshot check with :visible
  • Event-driven: react when the modal opens
  • Programmatic open / close
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with Bootstrap 5.3 and jQuery 3.7. Originally published 2022-08-23, rewritten and updated 2026-05-17.

TL;DR

// Snapshot check — is it open right now?
if ($('#myModal').is(':visible')) {
  console.log('Modal is open');
}

// Event-driven — run code when the modal opens
$('#myModal').on('show.bs.modal', function (e) {
  console.log('Modal is opening');
  // generate content, fetch data, etc.
});

// Useful Bootstrap modal events
// show.bs.modal     -- before fade-in starts
// shown.bs.modal    -- after fade-in completes
// hide.bs.modal     -- before fade-out starts
// hidden.bs.modal   -- after fade-out completes

Snapshot check with :visible

if ($('#myModal').is(':visible')) {
  // Modal is currently showing
} else {
  // Modal is hidden
}

jQuery’s :visible selector checks the actual rendered state — width, height, and display aren’t zero/none. For Bootstrap modals (which toggle display: block when open) it always returns the correct value.

Check if Bootstrap modal is open with jQuery — :visible snapshot and bs.modal events

Event-driven: react when the modal opens

$('#myModal').on('show.bs.modal', function (e) {
  // Fired BEFORE the modal animates in.
  // Good place to inject content that should appear with the fade.
  const trigger = e.relatedTarget;          // the button that opened it
  const userId  = $(trigger).data('user-id');

  $(this).find('.modal-title').text('User #' + userId);
});

$('#myModal').on('shown.bs.modal', function () {
  // Fired AFTER the fade-in completes.
  // Good place for autofocus or measurements.
  $(this).find('input[name="username"]').trigger('focus');
});

$('#myModal').on('hidden.bs.modal', function () {
  // Fired AFTER the modal is fully closed.
  // Good place to reset form state.
  $(this).find('form')[0].reset();
});

Programmatic open / close

// jQuery API (Bootstrap 4 and 5 both support this)
$('#myModal').modal('show');
$('#myModal').modal('hide');
$('#myModal').modal('toggle');

// Bootstrap 5 native (no jQuery)
const modal = bootstrap.Modal.getOrCreateInstance('#myModal');
modal.show();
modal.hide();

Frequently asked questions

Is $('#myModal').is(':visible') reliable on Bootstrap 5?

Yes — Bootstrap 5 still toggles a CSS class (show) and changes display on the modal element, so :visible works. An equivalent check that doesn’t depend on jQuery is document.getElementById('myModal').classList.contains('show'), which reads the actual Bootstrap state class directly.

Why use show.bs.modal instead of clicking-the-trigger handlers?

Because the modal can be opened in multiple ways — from a button, from JS calling .modal('show'), from another modal closing and yours auto-opening. show.bs.modal fires regardless of how the modal opened, so a single event handler covers every case. Button click handlers only cover the button case.

What’s the difference between show.bs.modal and shown.bs.modal?

show.bs.modal fires when the show starts — before CSS transitions run. The modal is in the DOM but the fade-in animation hasn’t completed. shown.bs.modal fires after the transition is done — the modal is fully visible and interactive. Use show to inject content that’s part of the open animation; use shown for focus management or measurements that need the final size.

Can I open or close the modal programmatically?

Yes — $('#myModal').modal('show') and $('#myModal').modal('hide'). 'toggle' flips whichever state you’re in. These work the same on Bootstrap 4 and 5 when jQuery is loaded; Bootstrap 5 also exposes a jQuery-free API: const m = bootstrap.Modal.getInstance('#myModal'); m.show().

Related guides

  • How to Check if an Element Is Hidden in jQuery
  • How to Check if a Checkbox Is Checked with jQuery
  • How to Add the Required Attribute to Input Fields with jQuery

References

Bootstrap 5 modal events: getbootstrap.com/docs/5.3/components/modal/#events.

TAGGED:bootstrapJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article JavaScript check HTTP referrer — document.referrer How to Check the HTTP Referrer with JavaScript
Next Article jQuery check if a checkbox is checked How to Check if a Checkbox Is Checked with jQuery
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Laravel Eloquent count rows — Post::count query snippet with aggregate bar chart icon
Web Development

How to Count Rows in Laravel Eloquent Efficiently

6 Min Read
Laravel migration column types cheat sheet
Web Development

Laravel Migration Column Types — Cheat Sheet

5 Min Read
WordPress disable revision and autosave — wp-config constants and wp_print_scripts dequeue
Web Development

How to Disable Revisions and Autosave in WordPress

8 Min Read
WordPress login user programmatically — wp_set_current_user plus wp_set_auth_cookie
Web Development

How to Login a User Programmatically in WordPress

8 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?