Reputation: 14108
Goal:
Close the bootstrap 5 modal with javascript code after displaying the alert message.
Problem:
I cannot make the function myFunction to be working and also closing the modal after displaying alert message with javascript code.
Info:
*You should not use 'data-bs-dismiss="modal"'
JSBIN:
https://jsbin.com/sileqajiho/edit?html,js,output
Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h3>Modal Example</h3>
<p>Click on the button to open the modal.</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
Open modal
</button>
</div>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" onclick="myFunction()" >Start</button>
</div>
</div>
</div>
</div>
<script>
function myFunction() {
alert("test")
}
<script>
</body>
</html>
var myModal = document.getElementById('staticBackdrop');
var modal = bootstrap.Modal.getOrCreateInstance(myModal)
modal.show()
Upvotes: 0
Views: 6650
Reputation: 11
for bootstrap 5.0 use the following code
var genericModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(genericModalEl)
modal.hide()
Upvotes: 0
Reputation: 14108
https://jsbin.com/ciqocavoge/edit?html,js,output
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-3">
<h3>Modal Example</h3>
<p>Click on the button to open the modal.</p>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
Open modal
</button>
</div>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<!-- Modal body -->
<div class="modal-body">
<button type="button" onclick="test1()">Close me via script</button>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" onclick="myFunction22()" >Start</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
function test1() {
alert("sdf");
modalInstance.hide()
}
</script>
</body>
</html>
-------------
var modalInstance = null;
var modelElem = document.querySelector('#myModal');
modelElem.addEventListener('shown.bs.modal', function (){
modalInstance = bootstrap.Modal.getInstance(modelElem);
});
Upvotes: 1
Reputation: 9
well I far as I know you have to use new , this is an example of a toast
var myToast = new bootstrap.Toast(toastDiV, opts)
myToast.show()
because they are classes you have to reference a new instance
Upvotes: -2