james
james

Reputation: 519

Bootstrap Modal not working(nothing happens when button clicked)

I have importaed javascript into my layout template:

<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"> 

I have the following bootstrap modal triggered by a button

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

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </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>

Did I forget to do something here? I have never done a bootstrap modal before but this should open a modal upon hitting the button correct? I thought it was because perhaps I was missing bootstrap javascript but I imported it from cloudflare min file and still doesn't do anything. What I am missing?

Upvotes: 2

Views: 4764

Answers (2)

user5447628
user5447628

Reputation:

Bootstrap JavaScript is written in a script tag, not a link one. The link one is for CSS.

Yours:

<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js">

It should be:

<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>

You also need to include jQuery like this

<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>

Otherwise your code is just fine!

Upvotes: 2

gffbss
gffbss

Reputation: 1701

You need to use a script tag for the bootstrap JS cdn not a link tag like so:

<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Also make sure you are using jQuery

Upvotes: 4

Related Questions