How to Open a Bootstrap Modal Window Using jQuery
Topic: Bootstrap / SassPrev|Next
Answer: Use the modal('show')
Method
You can simply use the modal('show')
method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide')
and modal('toggle')
.
Let's try out this example which programmatically open Bootstrap modal on a button click:
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Show Bootstrap Modal Window Using jQuery</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$(".btn").click(function(){
$("#myModal").modal('show');
});
});
</script>
<style>
.bs-example{
margin: 20px;
}
</style>
</head>
<body>
<div class="bs-example">
<!-- Button HTML (to Trigger Modal) -->
<button type="button" class="btn btn-lg btn-primary">Show Modal</button>
<!-- Modal HTML -->
<div id="myModal" class="modal fade" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>This is a simple Bootstrap modal. Click the "Cancel button", "cross icon" or "dark gray area" to close or hide the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: