I have a bootstrap modal dialog box that I want to show initially, then when the user clicks on the page, it disappears. I have the following:
$(function () {
$('#modal').modal(toggle)
});
<div class="modal" id='modal'>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Error:</h3>
</div>
<div class="modal-body">
<p>Please correct the following errors:</p>
</div>
</div>
</div>
The modal is displayed initially, but it does not close when clicked outside of the modal. Also, the content area is not greyed out.. How can I get the modal to display initially, then close after the user clicks outside the area? And how can I get the background to be grayed out as in the demo?
Add a button in modal with
data-dismiss="modal"
and hide the button withdisplay: none
. Here is how it will look likeNow when you want to close modal Programmatically just trigger a click event on that button, which is not visible to user
In Javascript you can trigger click on that button like this:
My five cents on this one is that I don't want to have to target the bootstrap modal with an id and seeing as there should be only one modal at a time the following should be quite sufficient to remove the modal as toggle could be dangerous:
In some circumstances typing error could be the culprit. For instance, make sure you have:
and not
Notice the double "ss" in the second example that will cause the Close button that fail.
In my case, main page from where bootstrap modal was triggered started to not responding after using
$("#modal").modal('toggle');
way, but started responding in the following way:In my case, I used a button to show the modal
So in my code, to close the modal (that has the id = 'my-modal-to-show') I call that function (in Angular typescript):
If I call $(modalId).modal('hide') it does't work and I don't know why
PS.: in my modal I coded that button element with .close class too