question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.hide() won't dismiss modal if fade class is present

See original GitHub issue

Prerequisites

Describe the issue

If a modal has the fade class on it then it won’t respond to a call to the .hide() method.

This modal

<div class="modal fade" id="loadingModal" tabindex="-1" aria-hidden="true">
  
  <div class="modal-dialog">
      
    <div class="modal-content">
        
      <div class="modal-header">
        <h5 class="modal-title" id="staticBackdropLabel">Loading </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      
      <div class="modal-body" id="loadingModalText">
        Loading JSON and HTML data for the document.
      </div>

    </div>
    
  </div>
  
</div>

Will respond to the .show() method but when called using the .hide() method it will not dismiss.

The only way to get this to work is to remove the fade class from the modal.

I am declaring the modal with this code

        this.loadingModal = new bootstrap.Modal(document.getElementById('loadingModal'), {
            backdrop:"static",
            keyboard:false,
            focus:true
        });

and then using this code (obviously in different functions) to show and hide it

this.loadingModal.hide();

this.loadingModal.show();

Reduced test cases

I tried to build a test case but CodPen and JS Bin have an issue with modal display.

I have this issue on two projects. One using Bootstrap via npm and the other using the code via the CDN.

What operating system(s) are you seeing the problem on?

Windows, macOS, iOS

What browser(s) are you seeing the problem on?

Safari, Firefox

What version of Bootstrap are you using?

Bootstrap v5.1.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GeoSotcommented, Nov 16, 2021

Hey @lolbat do me a favor. As your modal is animated you have to listen to the proper events before trigger the next action.

Try use something like:


this.loadingModal.show();
document.getElementById('loadingModal')l.addEventListener('shown.bs.modal', function (event) {
   this.loadingModal.hide();
})
0reactions
mdocommented, Feb 23, 2022

Revisiting this quite late, but I’m not sure what we need to add to the docs here as this 1) doesn’t seem super widespread of a problem, and 2) feels pretty standard when dealing with animations (you can’t interrupt them or act on an animating element until it’s done animating I believe).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable To Close Bootstrap Modal With Fade - Stack Overflow
JQuery's .load() is asynchronous. The problem is that when .modal("show") executes, the contents of your modal are not loaded yet.
Read more >
Modal · Bootstrap v5.0
It will slide down and fade in from the top of the page. ... When backdrop is set to static, the modal will...
Read more >
Bootstrap JS Modal Reference - W3Schools
The Modal plugin is a dialog box/popup window that is displayed on top of the current page. ... Use this class to add...
Read more >
10 Most Common Bootstrap Mistakes That Developers Make
Bootstrap modal appears under background ... If the modal container or its parent element has a fixed or relative position, the modal will...
Read more >
Bootstrap Modal - examples & tutorial
Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time. Nested modals aren't supported...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found