Don't forget that the data- attributes have changed to data-bs- in Bootstrap 5.

When the button is dynamically created, set the correct attributes:

const updateButton = addTextandAttributes(btn_update,
"<img src=\"resources/cloud-plus-fill.svg\"></img>", { 
  "type":"button",
  "class":"btn btn-outline-info btn-sm mt-1 me-2",
  "data-bs-toggle": "modal", 
  "data-bs-target": "#updateModal"
});
       

Demo


Modal Form not showing up

Answer from Carol Skelly on Stack Overflow
Discussions

Modal not showing
Data target needs a hashtag I think? And if you’re on Bootstrap 5 then it’s data-bs-target and data-bs-toggle. Hope that’s it, can’t see anything else but that’s because I’ve taken my glasses off. More on reddit.com
🌐 r/bootstrap
11
5
May 27, 2022
html - Bootstrap 5 modal not working with starter template - Stack Overflow
I'm trying to implement a simple modal using the Bootstrap 5 starter template (I'm using the Bootstrap Bundle with Popper option) but for some reason, it's not working. I've gone through other simi... More on stackoverflow.com
🌐 stackoverflow.com
Modal hide function is not working in bootstrap(v5)
At first, thanks a lot to provide the bootstrap 5 version I found not working hide() function in Modal Please, check this issue More on github.com
🌐 github.com
5
1
Bootstrap modal not displaying - Stack Overflow
For me, it was as stupid as a google search for "bootstrap modal" links to old documentation. Make sure to select your current version! 2021-10-28T09:15:23.647Z+00:00 ... Save this answer. Show activity on this post. I had the same problem. For me the cause was the use of Bootstrap 5 with outdated data-toogle and data-target attributes in the launch button: ... Get all my babies plus one. Here is it. 2022-07-08T19:30:19.273Z+00:00 ... Totally agree that most samples are outdated or not ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/bootstrap › modal not showing
r/bootstrap on Reddit: Modal not showing
May 27, 2022 -

Hey!

This is my first post here.

I usually code in Python, where make small flask projects(websites) that's when i use Bootstrap.

My issue right now is that i have a frontpage where i display some messages written on ad admin page. The admin has to be able to delete post, that's where i'm using a modal.

link to the code: https://pastebin.com/SunUyKnf

EDIT: Link to main template, base.html https://pastebin.com/3XMRMzFZ

the HTML also contains jinja for displaying backend data.

I actually got help maybe a week ago with the modal, because it wasnt selecting the id of the post to delete.

I fixed that with {{ post.id }}

After that it worked! i was able to delete posts from the frontpage using the modal it was great!, so i went on to work on the look of the webpage, since i had mostly just been doing the backend stuff first.

then 2 days ago i just wanted to test it for a reason i can't remeber, and then it freakin didn't work anymore .I have been trying to solve the issue on and off for about 2 days.

I am about to loose my mind over this issue.

first it worked then it didn't and i havent touched the page since it worked, i don't get it.

I know it's modal that 's the problem because i've tested my backend and it works.

delete route:

@/app.route('/home/<int:post_id>/delete', methods=['POST','GET'])
@/login_required 
def delete_post(post_id): 
post = Post.query.get(post_id) 
if current_user.is_authenticated: 
db_session.delete(post) 
db_session.commit() 
flash('Post has been deleted','success') 
return redirect(url_for('home')) 
return redirect(url_for('home'))

If i put in the URL /home/post.id/delete and press enter it will delete the post matching the id.

I thought this was the right place to ask, even though there is a bit of Python involved.

i sincerely hope somone here is able to help me, an i well apologies in advance if it's just some lame stupid mistake that i completety missed.

🌐
Codeply
codeply.com › p › qikoyVgKHL
Bootstrap 5 modal not working (solved) on Codeply
HTML, CSS, JavaScript editor playground for designers & developers to compare, prototype and test frontend frameworks
🌐
Bootstrap
getbootstrap.com › docs › 5.2 › components › modal
Modal · Bootstrap v5.2
Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more.
Top answer
1 of 1
21

You're probably using some older example/template. Try to use code from the documentation

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"/>
    <title>Hello, world!</title>
  </head>
  <body>
    <div class="container">
      <h2>Basic Modal Example</h2>
      <!-- Button trigger modal -->
      <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
        Launch demo modal
      </button>

      <!-- Modal -->
      <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
              ...
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
  </body>
</html>

Find elsewhere
🌐
W3Schools
w3schools.com › bootstrap5 › bootstrap_modal.php
Bootstrap 5 Modal
Change the size of the modal by adding the .modal-sm class for small modals (max-width 300px), .modal-lg class for large modals (max-width 800px), or .modal-xl for extra large modals (max-width 1140px). Default is 500px max-width.
🌐
MDBootstrap
mdbootstrap.com › standard › material design for bootstrap 5 & vanilla javascript
Modal not working - Material Design for Bootstrap
<div class="fixed-action-btn" ... bundle, and also copy/pasted codes directly from the examples. If I open the file and remove display: none, the modal shows fine, but I can't close nor trigger it from the button....
🌐
YouTube
youtube.com › bytegrad
Bootstrap 5 Modal myModal.show() not working JavaScript (SOLVED) - YouTube
👉 Professional JavaScript Course: https://bytegrad.com/courses/professional-javascript👉 Professional CSS Course: https://bytegrad.com/courses/professional-...
Published   May 29, 2019
Views   10K
🌐
GitHub
github.com › orgs › twbs › discussions › 32347
Modal hide function is not working in bootstrap(v5) · twbs · Discussion #32347
I'm working on a side project wherein I'm using Bootstrap 5, Vue 3, and Vue's Composition API. I have written a utility method, toggleModal(), to handle the showing and hiding of the modal. modal.hide() is not responding accordingly when the save button in the template is clicked; it only closes the modal when data-dismiss="modal" is added to the button element, and even then the modal is closed before the async call to the API is completed.
🌐
ASPSnippets
aspsnippets.com › questions › 139468 › Solved-Bootstrap-5-modal-popup-not-displaying-using-jQuery
Solved Bootstrap 5 modal popup not displaying using jQuery
HiI used bootstrap 5 modal for CRUD operation Now I want use js for edit data from js file When I click edit button not working modalcountryjs39document39readyfunction 39table editButton39on39click39 functionevent eventpreventDefault countriesfindByIdid1 var href thisattr39href39 gethref ...
Top answer
1 of 1
1

The code you posted above is working fine. Check your HTML and CSS.

Copy<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div class="col-12 offset-md-10">
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdropLive"> Launch static backdrop modal
  </button>
</div>

<div class="modal fade" id="staticBackdropLive" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLiveLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="staticBackdropLiveLabel">Modal title</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
        </button>
      </div>
      <div class="modal-body">
        <p>I will not close if you click outside of me. Don't even try to press escape key.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Understood</button>
      </div>
    </div>
  </div>
</div>
Run code snippetEdit code snippet Hide Results Copy to answer Expand


EDIT

You probably didn't include Bootstrap 5, but Bootstrap 4 into your project.

Bootstrap 4:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>

Bootstrap 5:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
Top answer
1 of 9
66

The modal plugin toggles your hidden content on demand, via data attributes or JavaScript. It also adds .modal-open to the to override default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal. [source]

How to use via Vanilla JavaScript

Create a modal with a single line of JavaScript:

var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)

source

A quick example:

Copyvar myModal = new bootstrap.Modal(document.getElementById("exampleModal"), {});
document.onreadystatechange = function () {
  myModal.show();
};
Copy<!DOCTYPE html>
<html lang="en">
  <head>

    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />


    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css"
      integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I"
      crossorigin="anonymous"
    />
    <title>Hello, world!</title>
  </head>
  <body>

    <div
      class="modal fade"
      id="exampleModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="exampleModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button
              type="button"
              class="btn btn-secondary"
              data-dismiss="modal"
            >
              Close
            </button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    <!-- JavaScript and dependencies -->
    <script
      src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
      integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"
      integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"
      crossorigin="anonymous"
    ></script>

    <script src="./app.js"></script>
  </body>
</html>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

As for your code, you didn't follow the right way to show/toggle a modal in Vanilla JavaScript. Why did you expect myModal.show() to happen while myModal is just a DOM element?

2 of 9
38
  1. Create a new instace of modal and then call show method(see usage points)
Copyvar myModal = new bootstrap.Modal(document.getElementById('exampleModal'))
myModal.show()

ref: https://v5.getbootstrap.com/docs/5.0/components/modal/#options

note: Bootstrap v5 no more uses jquery, instead it uses javascript. lots of changes are there in bootstrap v5, if you are working for a company and willing to use Bootstrap v5 then please go through all the changes.

🌐
Bootstrap
getbootstrap.com › docs › 5.3 › components › modal
Modal · Bootstrap v5.3
Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more.
🌐
Team Treehouse
teamtreehouse.com › community › modal-not-displaying-although-i-followed-the-video-exactly
Modal not displaying although I followed the video exactly (Example) | Treehouse Community
December 31, 2022 - I was having the same issue, but ... data-bs-target="#register">Register Now</button> If you update the cdn links, or just remove "bs-" from the two attributes, it should work....
Top answer
1 of 15
195

Check the version of bootstrap. Take attention that in bootstrap 5 changed data-toggle to data-bs-toggle and data-target to data-bs-target

2 of 15
34

have you used the cdn or file popper.js If you not include popper.js file you wont be able to see your modal windows check the below cdn and put below jquery.js and bootstrap.js as follows below

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

Now it will work 100% if not I have another solution copy the below code and check it

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
🌐
Reddit
reddit.com › r/css › [bootstrap]modal not showing
r/css on Reddit: [Bootstrap]Modal not showing
January 6, 2016 -

I feel like this should be rather simple, but I can't figure it out and it's starting to drive me crazy.

I am using Bootstrap (on the Java Play framework if that matters) and trying to make a simple modal. I copied a tutorial I found online, but for some reason the modal just won't show.

Here is the portion of code in question:

<body>
    <div id="Header">
        <div class="row text-right">
            <a id="login-button" data-toggle="modal" data-target="#loginModal" type="button">Login</a>
        </div>
    </div>
        <!-- Modal -->
    <div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModal" 
                     aria-hidden="true">
        <div class="modal-dialog modal-sm">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" 
                                          aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Small Modal</h4>
                </div>
                <div class="modal-body">
                    <h3>Modal Body</h3>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>

        </div>
    </div>
    @content
</body>

Can anyone see what might be causing it to fail?

Edit: So I was looking at the CSS and for some reason the Bootstrap .modal class has display:none and .fade has opacity defaulted to 0. I can mess around in the browser tools and change that, but i'm not sure how to fix it permanently.