New Post has been published on Html Use
New Post has been published on http://www.htmluse.com/twitter-bootstrap-modals/
Creating Modals with Twitter Bootstrap
Modals are basically a dialog box that is used to provide important information to the user or prompt user to take necessary actions before moving on. Modal windows are widely used to warn users for situations like session time out or to receive their final confirmation before going to perform any critical actions such as saving or deleting important data.
Using Twitter Bootstrap you can create very smart and flexible dialog boxes with the minimum efforts. The following example shows you how to create a simple modal with a header, message body and the footer containing action buttons for the user.
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă</button>
<h4 class="modal-title">Confirmation</h4>
<p>Do you want to save changes you made to document before closing?</p>
<p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
â The above example launches the modal window when the DOM is fully loaded via JavaScript. The output will look something like this:
Activate Modals via Data Attributes
You can activate a Twitter Bootstrap modal window by clicking on the button or link without writing any JavaScript code via data attributes. Set data-toggle="modal" on a controller element, like a button or an anchor, along with a data-target="#myModal" or href="#myModal" to target a specific modal box.
<!-- Button HTML (to Trigger Modal) -->
<a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă</button>
<h4 class="modal-title">Confirmation</h4>
<p>Do you want to save changes you made to document before closing?</p>
<p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
The above example launches the modal window on click of âLaunch Demo Modalâ button via data attributes of Bootstrap modal.
Activate Modals via JavaScript
You may also activate a Twitter Bootstrap modal window via JavaScript â just call the modal() Bootstrap method with the modal "id" or "class" selector in your JavaScript code.
<script type="text/javascript">
$(document).ready(function()
$(".btn").click(function()
$("#myModal").modal('show');
Changing the Sizes of Modals
Bootstrap gives you option further to scaling a modal up or down. You can a make modals larger or smaller by adding an extra class .modal-lg or .modal-sm on .modal-dialog.
<button class="btn btn-primary" data-toggle="modal" data-target="#largeModal">Large modal</button>
<div id="largeModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă</button>
<h4 class="modal-title">Large Modal</h4>
<p>Add the <code>.modal-lg</code> class on <code>.modal-dialog</code> to create this large modal.</p>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">OK</button>
<button class="btn btn-primary" data-toggle="modal" data-target="#smallModal">Small modal</button>
<div id="smallModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă</button>
<h4 class="modal-title">Small Modal</h4>
<p>Add the <code>.modal-sm</code> class on <code>.modal-dialog</code> to create this small modal.</p>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">OK</button>
There are certain options which may be passed to modal() Bootstrap method to customize the functionality of a modal window.
Name Type Default Value Description backdrop boolean
or the string 'static' true Includes a modal-backdrop (black overlay area) element. Alternatively, you may specify static for a backdrop which doesnât close the modal on click. keyboard boolean true Closes the modal window on press of escape key. show boolean true Shows the modal when initialized or activate. remote URL false If a remote url is provided, content will be loaded via jQueryâs load method and injected into the root of the modal element.
You can also set these options using the data attributes on modal box â just append the option name to data-, like data-backdrop="static", data-keyboard="false" etc.
If youâre using the data api for setting the options for modal window, you may alternatively use the âhrefâ attribute to provide the URL of remote source like:
<!-- Button HTML (to Trigger Modal) -->
<a href="remote.html" role="button" class="btn btn-large btn-primary" data-toggle="modal" data-target="#myModal">Launch Demo Modal</a>
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<!-- Content will be loaded here from "remote.php" file -->
Tip:You can prevent the modal box form closing when a user click on the backdrop (black overlay area) by setting the value of data attribute data-backdrop to static like data-backdrop="static".
These are the standard bootstrapâs modals methods:
This method activates your content as a modal.
<script type="text/javascript">
$(document).ready(function()
$(".launch-modal").click(function()
This method toggles a modal window manually.
<script type="text/javascript">
$(document).ready(function()
$(".toggle-modal").click(function()
$("#myModal").modal('toggle');
This method can be used to open a modal window manually.
<script type="text/javascript">
$(document).ready(function()
$(".open-modal").click(function()
$("#myModal").modal('show');
This method can be used to hide a modal window manually.
<script type="text/javascript">
$(document).ready(function()
$(".hide-modal").click(function()
$("#myModal").modal('hide');
Bootstrapâs modal class includes few events for hooking into modal functionality.
Event Description show.bs.modal This event fires immediately when the show instance method is called. shown.bs.modal This event is fired when the modal has been made visible to the user. It will wait until the CSS transition process has been fully completed before getting fired. hide.bs.modal This event is fired immediately when the hide instance method has been called. hidden.bs.modal This event is fired when the modal has finished being hidden from the user. It will wait until the CSS transition process has been fully completed before getting fired.
The following example displays an alert message to the user when fade out transition of the modal window has been fully completed.
<script type="text/javascript">
$(document).ready(function()
$("#myModal").on('hidden.bs.modal', function()
alert("Modal window has been completely closed.");