Bootstrap Modal doesn't seem to work with Anchor Tag

1564 views html
3

I am using modals for some stuff in my html. I tried to use it with the anchor tag but to no success. What am I doing wrong? Here is my code:

     <a data-toggle="modal" href="#myModal">ANCHOR</a>

                                  <!-- Modal -->

      <div id="myModal" class="modal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">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">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>   

And yes, the other Bootstrap things are working perfectly.

I even tried to put the modal stuff at the end but it did nothing.

answered question

1 Answer

3

You need to add the id, myModal to the modal you are trying to target:

     <a data-toggle="modal" href="#myModal">ANCHOR</a>

                                  <!-- Modal -->

      <div id="myModal" class="modal">            <!-- add id here -->
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">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">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

posted this

Have an answer?

JD

Please login first before posting an answer.