YouTip LogoYouTip

Bootstrap Modal Plugin

A modal is a child window that overlays the parent window. Typically, the purpose is to display content from a separate source, allowing for some interaction without leaving the parent window. The child window can provide information, interaction, etc. > !(#) If you want to use the plugin's functionality individually, you need to include **modal.js**. Or, as mentioned in the (#) chapter, you can include _bootstrap.js_ or the minified version _bootstrap.min.js_. ## Usage You can toggle the hidden content of the Modal plugin: * **Using data attributes**: Set the attribute **data-toggle="modal"** on a controller element (like a button or link), and set **data-target="#identifier"** or **href="#identifier"** to specify the particular modal (with id="identifier") to toggle. * **Using JavaScript**: With this technique, you can call a modal with id="identifier" using a simple line of JavaScript: `$('#identifier').modal(options)` ### Example A static modal window example, as shown below: ## Example

Creating a Modal

[Try it Β»](#) The result is as follows: ![Image 2: Bootstrap Modal Plugin](#) **Code Explanation:** * To use a modal, you need some kind of trigger. You can use a button or a link. Here we are using a button. * If you look closely at the code above, you will see that in the `
[Try it Β»](#) The result is as follows: ![Image 3: Bootstrap Modal Plugin Methods](#) Just press the ESC key, and the modal window will exit. ## Events The following table lists the events 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 () { // Do something...})` | | shown.bs.modal | Triggered when the modal is visible to the user (will wait for CSS transitions to complete). | `$('#identifier').on('shown.bs.modal', function () { // Do something...})` | | hide.bs.modal | Triggered when the `hide` instance method is called. | `$('#identifier').on('hide.bs.modal', function () { // Do something...})` | | hidden.bs.modal | Triggered when the modal is completely hidden from the user. | `$('#identifier').on('hidden.bs.modal', function () { // Do something...})` | ### Example The following example demonstrates the usage of the events: ## Example

Bootstrap Modal Plugin Events

$(function() { $('#myModal').modal('hide') }); $(function() { $('#myModal').on('hide.bs.modal', function() { alert('Hey, I heard you like modals...'); }) }); [Try it Β»](#) The result is as follows: ![Image 4: Bootstrap Modal Plugin Events](#) As shown in the example above, if you click the _Close_ button, which is the _hide_ event, an alert message will be displayed.
← Bootstrap Dropdown PluginBootstrap Transition Plugin β†’