Reputation: 197
when I try to load a modal in Bootstrap, it shows up when I refresh the page but doesn't show up when I click on the button. I'd like my modal to pop up only when the user clicks on the button.
My html file is set up as follows:
<head>
<meta charset="utf-8">
<title>my title here</title>
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
<script src="/static/js/jquery-3.2.1.min.js"></script>
<script src="/static/js/popper.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/Highcharts-5.0.14/code/highcharts.js"></script>
<script src="/static/Highcharts-5.0.14/code/modules/exporting.js"></script>
<!-- Our Custom CSS -->
<link rel="stylesheet" href="/static/css/style_charts_black.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
I've copied this code from the Bootstrap tutorial website:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" style="float:right; margin-right:100px">
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">
<h5 class="modal-title" id="myModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>$('#myModal').modal('show')</script>
I do not get any jquery errors in the javascript console. I've also tried to change the last line above to .modal('toggle') but it didn't help.
Upvotes: 0
Views: 29701
Reputation: 243
Just remove the data toggle in the html tag and write the
$('#myModal').modal('show')
or
$('#myModal').modal('toggle')
Upvotes: 1
Reputation: 191
Your code works fine, if you just want the modal to be activated when you click the button, you do not need the script
$('#myModal').modal('show')
<head>
<meta charset="utf-8">
<title>GSMA Intelligence — Visuals</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<!-- Button trigger modal -->
<!-- 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">×</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>
Upvotes: 0
Reputation: 2617
Your code is fine, you need to include the js files in the <body>
tag, not the head.
From their documentation:
Add our JavaScript plugins, jQuery, and Tether near the end of your pages, right before the closing body tag.
Here is the link where I found that info:
https://v4-alpha.getbootstrap.com/getting-started/introduction/
And a codepen of the js files included in the body and your code working correctly:
https://codepen.io/egerrard/pen/MEGgJE
Upvotes: 0