I included a Bootstrap modal window in the HTML code of my webpage and I display it when a particular event occurs (a text field isn't empty and the string doesn't match any of the values in a JSON array).
The modal is displayed correctly when the event occurs. But the close
button doesn't work, neither does the "X" button.
Shouldn't the buttons of a Bootstrap modal window work by default or should I add some other function to let them do the task?
This is the HTML code where I inserted the modal:
<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Error</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div id="testoMyModal" class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
And the following are the JAVASCRIPT snippets where the modal is called:
1)
function validateCitta() {
let text = $('#inlineFormInputCitta').val();
if (text === "") {
$("#errorLog").show();
}
else {
$.ajax({
url: "https://nominatim.openstreetmap.org/search?q=" + encodeURIComponent($("#inlineFormInputCitta").val()) + "&format=geocodejson",
dataType: "json",
success: function (data) {
var check = false;
for (let i = 0; i < data.features.length; i++) {
let typeCity = data.features[i].properties.geocoding.type;
if (typeCity === "city") {
let nameCity = data.features[i].properties.geocoding.name;
for (let i = 0; i < json.tappe.length; i++) {
let tappa = json.tappe[i];
let city = json.tappe[i].city;
if (city === nameCity) {
console.log("JSON file has been activated");
check = true;
$("#tbody").append("<tr><td>" + tappa.name + "</td><td>" + tappa.state + "</td><td>" + tappa.region + "</td><td>" + tappa.city + "</td></tr>");
$("#tabella").show();
}
;
}
;
}
}
if (!check) {
$('#myModal').show();
}
}
})
}
};
2)
function hideTutto() {
$('#myModal').hide();
};
Is it normal that these Modal buttons don't work by default? If not, why don't they?
- E D I T [ S O L V E D ]
The issue came from a syntax error:
I wrote $('#myModal').show();
instead than $('#myModal').modal('show');
source: Modals methods Now the buttons work.