kneidels
kneidels

Reputation: 914

Bootstrap 5 modal not closing

I must be doing something silly.

I am programmatically opening a modal using $("#exampleModal").show();

The modal opens correctly. But for some reason - i do not get the dark overlay behind the modal - and the 2 close buttons do not work.

I had the class 'fade' applied as in the sample, but it meant the modal would not open at all.

JSFIDDLE: https://jsfiddle.net/kneidels/x9zwfumt/

Upvotes: 1

Views: 542

Answers (1)

Rok Benko
Rok Benko

Reputation: 22890

Bootstrap 5:

Change $("#exampleModal").show(); to $("#exampleModal").modal("show");.

It solves everything you mentioned.

See the snippet below.

$(function() {

  $(".triggerlist").click(function() {
    $("#exampleModal").modal("show");
  })

});
body {
  font-size: 1.0rem;
  padding: 20px;
}

.triggerlist {
  text-decoration: underline;
  color: blue;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

<h1>Hello, world!</h1>

<p class="triggerlist">The packages will be sent to<span class="d-none d-lg-inline">:</span> </p>

<div class="modal " id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">

  <div class="modal-dialog modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Package Recipients</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        TEST CONTENT
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 4

Related Questions