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?
Running this call via class
($(".my-modal"))
won't work.After some test, I found that for bootstrap modal need to wait for some time before executing the
$(.modal).modal('hide')
after executing$(.modal).modal('show')
. And i found in my case i need at least 500 milisecond interval between the two.So this is my test case and solution:
Try This
Another way of doing this is that first you remove the class modal-open, which closes the modal. Then you remove the class modal-backdrop that removes the grayish cover of the modal.
Following code can be used:
to close bootstrap modal you can pass 'hide' as option to modal method as follow
Please take a look at working fiddle here
bootstrap also provide events that you can hook into modal functionality, like if you want to fire a event when the modal has finished being hidden from the user you can use hidden.bs.modal event you can read more about modal methods and events here in Documentation
If none of the above method work, give a id to your close button and trigger click on close button.
$('.modal.in').modal('hide');
use the above code to hide the backdrop of modal if you are using multiple modal pop in one page.