6

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>

5 Answers 5

7

This one works for Bootstrap 5

new bootstrap.Modal(document.querySelector("#exampleModalCenter")).show();
Sign up to request clarification or add additional context in comments.

Comments

6

Use .modal('show').

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

Extracted from Bootstrap Modal API

4 Comments

<script> $('#exampleModalCenter').modal('show'); </script> This is enough? (I am using bootstrap). Thanks.
@julian Seems alright as long as you have jQuery + Bootstrap loaded on your html page
Button open the modal, but this code doesn´t open. Why?
Now ok: <script> $(document).ready(function(){ $('#exampleModalCenter').modal('show'); }); </script> Thanks Liad Yogev!!
3

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";

1 Comment

While this answer works, it breaks the normal transitions that happen in a Bootstrap theme and requires additional unnecessary coding.
0
const modalElement = document.getElementById('yourmodalid');
if(modalElement){ 
    const modal = new bootstrap.Modal(modalElement); modal.show(); 
}

1 Comment

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. And maybe highlight the relevant additional original insight when compared to existing answers.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.