最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Bootstrap | Open bootstrap modal with javascript and not with button - Stack Overflow

programmeradmin4浏览0评论

How can we open bootstrap modal with javascript and not with button? I need to open without button, because another javascript program will decide when to open the modal.

JAVASCRIPT:

<script>
  //...
  document.getElementById("modalElement").style.visibility = ""; 
</script>

HTML:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
  Launch demo modal
</button>

<!-- Modal -->
<div id="modalElement" class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">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>

How can we open bootstrap modal with javascript and not with button? I need to open without button, because another javascript program will decide when to open the modal.

JAVASCRIPT:

<script>
  //...
  document.getElementById("modalElement").style.visibility = ""; 
</script>

HTML:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
  Launch demo modal
</button>

<!-- Modal -->
<div id="modalElement" class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">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>
Share Improve this question edited May 30, 2020 at 12:28 user11141611 asked May 30, 2020 at 11:18 retret 2391 gold badge2 silver badges9 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

Use .modal('show').

With jQuery it will be: $('#modalElement').modal('show')

Extracted from Bootstrap Modal API

This one works for Bootstrap 5

new bootstrap.Modal(document.querySelector("#exampleModalCenter")).show();

This is for Bootstrap 5 but should work in lower versions as well.

document.querySelector(".modal").classList.add("show");
document.querySelector(".modal").style.display = "block";
const modalElement = document.getElementById('yourmodalid');
if(modalElement){ 
    const modal = new bootstrap.Modal(modalElement); modal.show(); 
}

answer using purely javascript

    var modalElement = new bootstrap.Modal(document.getElementById('modalElement'))

https://getbootstrap.com/docs/5.0/components/modal/#via-javascript

or if your calling it asynchronously

    const modalElement = bootstrap.Modal.getOrCreateInstance('#modalElement');
    modalElement.show();

https://getbootstrap.com/docs/5.0/components/modal/#getorcreateinstance

btw. you have two id attributes there. you must have only one id attribute in your element

发布评论

评论列表(0)

  1. 暂无评论