Easy Tutorial
❮ Bootstrap Button Plugin Bootstrap Alerts ❯

Bootstrap Modal Plugin

The Modal is a child window that is overlaid on top of the parent window. Typically, the purpose is to display content from a separate source that can include some interaction without leaving the parent window. The child window can provide information, interaction, etc.

modal.js. Alternatively, as mentioned in the Bootstrap Plugin Overview chapter, you can include bootstrap.js or the compressed bootstrap.min.js.

Usage

You can toggle the hidden content of the Modal plugin:

Example

A static modal window example, as shown in the following example:

Example

<h2>Creating a Modal</h2>
<!-- Button to trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Start Demo Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Modal Title</h4>
            </div>
            <div class="modal-body">Add some text here</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Submit Changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

The result is as follows:

Code Explanation:

-data-dismiss="modal" is a custom HTML5 data attribute used to close the modal window.

-class="modal-body" is a CSS class from Bootstrap CSS used to style the body of the modal window.

-class="modal-footer" is a CSS class from Bootstrap CSS used to style the footer of the modal window.

-data-toggle="modal" is a custom HTML5 data attribute used to open the modal window.

Options

Some options can be used to customize the appearance and behavior of the modal window (Modal Window), which are passed through data attributes or JavaScript. The following table lists these options:

Option Name Type/Default Value Data Attribute Name Description
backdrop boolean or string 'static' <br> Default: true data-backdrop Specifies a static backdrop, which prevents the modal from closing when the user clicks outside of it.
keyboard boolean <br> Default: true data-keyboard Closes the modal when the escape key is pressed; setting it to false disables this behavior.
show boolean <br> Default: true data-show Shows the modal when initialized.
remote path <br> Default: false data-remote Uses jQuery .load method to inject content into the modal's body. If an href with a valid URL is added, its content will be loaded, as shown in the example below: <a data-toggle="modal" href="remote.html" data-target="#modal" rel="noopener noreferrer">Please click me</a>

Methods

Here are some useful methods that can be used with modal().

Method Description Example
Options:.modal(options) Activates the content as a modal. Accepts an optional options object. $('#identifier').modal({<br>keyboard: false<br>})
Toggle:.modal('toggle') Manually toggles the modal. $('#identifier').modal('toggle')
Show:.modal('show') Manually opens the modal. $('#identifier').modal('show')
Hide:.modal('hide') Manually hides the modal. $('#identifier').modal('hide')

Example

The following example demonstrates the usage of methods:

Example

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <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" id="myModalLabel">Modal Title</h4>
            </div>
            <div class="modal-body">Press the ESC key to exit.</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
```html
<button type="button" class="btn btn-primary">Submit Changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->

The result is as follows:

Simply press the ESC key, and the modal window will exit.

Events

The table below lists the events to be used in the modal. These events can be used as hooks in functions.

Event Description Example
show.bs.modal Triggered after the show method is called. $('#identifier').on('show.bs.modal', function () {<br> // Perform some actions...<br>})
shown.bs.modal Triggered when the modal is visible to the user (will wait for the CSS transition to complete). $('#identifier').on('shown.bs.modal', function () {<br> // Perform some actions...<br>})
hide.bs.modal Triggered when the hide instance method is called. $('#identifier').on('hide.bs.modal', function () {<br> // Perform some actions...<br>})
hidden.bs.modal Triggered when the modal is completely hidden from the user. $('#identifier').on('hidden.bs.modal', function () {<br> // Perform some actions...<br>})

Example

The following example demonstrates the usage of events:

Example

<!-- Modal -->
<h2>Modal Plugin Events</h2>
<!-- Button to trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Start Demo Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <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" id="myModalLabel">Modal Title</h4>
            </div>
            <div class="modal-body">Click the close button to check the event functionality.</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Submit Changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script>
$(function() {
    $('#myModal').modal('hide')
});
</script>
<script>
$(function() {
    $('#myModal').on('hide.bs.modal',
    function() {
        alert('Hey, I heard you like modals...');
    })
});
</script>

The result is as follows:

As shown in the example above, if you click the Close button, which is the hide event, an alert message will be displayed.

❮ Bootstrap Button Plugin Bootstrap Alerts ❯