I have looked at the questions Capture close event on Bootstrap Modal and Bind a function to Twitter Bootstrap Modal Close but didn't quite solve my problem as stated in the title.
The questions differ in (1) BootStrap version as eventListener seems have changed; (2) with and without JQuery.
Here is my Modal snippet:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Hello Modal
<input type="text" id="txtUserInput">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<script>
const myModalx = new bootstrap.Modal(document.getElementById('myModal'), {
keyboard: false
});
myModalx.addEventListener('hidden.bs.model', event => {
// do something...
})
</script>
The BS-5.2 CSS for the code:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
Wrap with HTML head stuff, I got an error in browser's console. (Firefox in my case).
Uncaught TypeError: myModalx.addEventListener is not a function
I looked at the BS-5.2 doc, it actually mentions a method to handle the hide event
myModal.hide()
Any suggestion in how to use that method, or whatever event handler to return the value from the Modal?