I tried to make a modal. I use AJAX to show the modal but the modal is invisible. When I use the button’s trigger with data-toggle and data-target, the modal is showing up visible.
This is what I have tried to do
- Check that all script is correctly loaded
- Tried to reorder script
- Delete fade class in modal and the modal visible but with bad UI
- Change bootstrap version but result is same
This is my code
AJAX
$("body").on("click", ".btn-show", function (event) {
event.preventDefault();
var me = $(this),
url = me.attr("href"),
title = me.attr("title");
$("#modal-title").text(title);
$.ajax({
url: url,
dataType: "html",
success: function (response) {
$("#modal-body").html(response);
$("#modal").modal("show");
},
});
});
HTML Modal
<div class="modal fade" id="modal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" id="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="modal-title">Form Input</h4>
</div>
<div class="modal-body" id="modal-body">
</div>
<div class="modal-footer" id="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary" id="modal-btn-save">Save
changes
</button>
</div>
</div>
</div>
</div>
SS modal exists but is not visible:
3
Answers
There are no errors with the modal, and the
$("#modal").modal("show");
displays the modal just fine (see code snippet below),Since the code snippet below works fine, it seems clear that the original code’s “success” function is never called:
I suggest adding an error function to the ajax parameters:
From the jQuery docs:
I just fix this problem on Laravel Voyager Admin.
This is because I just declare two times bootstrap scripts.
I had the same issue, in my case the code relied on the "fade in" animation to properly display the modal and my code only had the "fade" class. So, while it was adding the class "show" to the modal, the property would stay with opacity 0 due to the animation overlapping it.
Original code with bug:
My fix: