Reputation: 365
I am experiencing a very strange issue with Bootstrap Modal:
The first time I load my web page with browser cache cleared and click on an icon that should open
the modal it does not open. When I refresh the page and click the icon, the modal appears... When I close the modal and click the icon again it works again.
This is my header code:
<link href="libs/jquery/jquery-ui.css" rel="stylesheet" />
<link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="libs/jquery/jquery-2.1.4.min.js"></script>
<script src="libs/jquery/jquery-migrate-1.2.1.min.js"></script>
<script src="libs/jquery/jquery-1.11.3.min.js"></script>
<script>$(document).ready(function() { $.noConflict(); )};</script>
<script src="libs/bootstrap/js/bootstrap.min.js"></script>
<script src="libs/jquery/jquery-ui.js"></script>
This is my body code:
<a id="myIcon">Open Modal</a>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">Body</div>
<div class="modal-footer">Footer</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
And for those who want to see the full code, this is my JS:
$(document).ready(function() {
$('#myIcon').click(function () {
$('#myModal').modal('show');
}
});
Upvotes: 3
Views: 6307
Reputation: 3559
I know what is happening. You are forgetting set some attributes in your anchor.
<a id="myIcon" data-toggle="modal" data-target="#myModal">Open Modal</a>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">Body</div>
<div class="modal-footer">Footer</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
You could avoid your script. In this case your script is simply simulating a click on the modal. Bootstrap already has an event associated to the buttons which have the attributes that I have wrote in your anchor.
$(document).ready(function() {
// You are triggering a click in your button
$('#myIcon').click(function () {
// After the click, you force the modal to be shown
$('#myModal').modal('show');
}
});
You can allow only JS interactions with modals. This way, modals will not have an associated event.
<a class="open_modal" data-modal="myModal">Open Modal</a>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">Body</div>
<div class="modal-footer">Footer</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
You remove the anchor's attributes, and then:
$(document).ready(function() {
$('.open_modal').on('click',function () {
var idModal = $(this).data("modal")
$(idModal).modal('show');
}
});
This way, if a .open_modal element is clicked, its data-modal, that represents a modal id, it's used to show the modal.
Upvotes: 1