I was stuck on this for a while also. I have no idea why but when I used data-bs-dismiss
in stead of data-dismiss in the class for the close button it worked for me.
data-dismissis on Bootstrap 4.x version whiledata-bs-dismissis on Bootstrap 5.x version.
Please see below full code for close button.
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
Answer from Colmofarrell on Stack OverflowI was stuck on this for a while also. I have no idea why but when I used data-bs-dismiss
in stead of data-dismiss in the class for the close button it worked for me.
data-dismissis on Bootstrap 4.x version whiledata-bs-dismissis on Bootstrap 5.x version.
Please see below full code for close button.
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
remove the "fade" class.
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
change to
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
Fade is an effect, if you remove fade, your modal will not have fade effect. The several problem which cause this issue is not call modal with right method.
Wrong method : $("#myModal").show();
Right method : $("#myModal").modal('show');
Videos
The hiding functionality is implemented in the modal.js in this way.
this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
Basically it's just finding the elements that have the attribute of data-dismiss and the value of modal. Upon click it will hide these elements.
Replace data-dismiss="modal" by: onclick="$('#modal_id').modal('hide');"
You should have something like this:
<button type="button" class="close" onclick="$('#modal_id').modal('hide');" aria-label="Close">
onclick="$('#modal_id').modal('hide');" will close only the particular modal in which it is placed.
I was able to figure out my issue. It was that in my own JS file, I wanted to add into every button click event, event.stopPropagation(); Since this was a method, and I did not want this added multiple times to the same button every time it was called, I would remove the previous click events, which was removing Bootstrap's dismiss click event.
So, if you have this same issue and you have all of Bootstrap's JS added correctly, check your own code to make sure you're not overwriting/removing events.
UPDATE
Just to clarify, event.stopPropagation() prevents data-dismiss from working.
if you put into your code (in some cases it makes sense):
$modal.off('click')
then it also results that the modal won't be closed because the event is blocked.
In that case you have to handle it yourself:
$modal
.on('click', '[data-dismiss="modal"]', function() {
$modal.modal('hide');
});
Remove data-dismiss="modal" from the button and Using Jquery you can close a particular modal.Create a function and have the below code inside the function and call that function on the close button click
$('#modalid').modal('hide');
That works for me. Only Close that modal. No extra function.
<button type="button" class="close" onclick="$('#my_modal').modal('hide');" aria-label="Close">