Bootstrap Alert Plugin
Alert messages are mostly used to show end users information such as warnings or confirmation messages. With the Alert plugin, you can add dismiss functionality to all alert messages.
Usage
You can enable the dismissal functionality of alerts in the following two ways:
- Via data attributes: To add dismissal via the Data API, simply add data-dismiss="alert"to the close button, and the alert will automatically get close functionality.
$(".alert").alert()
Example
The following example demonstrates using the Alert plugin via data attributes.
Example
<div class=">alert alert-warning
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Warning!
strong
There is a problem with your network connection.
div
Try it »
The result is as follows:
Option
No options.
Method
The following are some useful methods of the Alert plugin:
| Method | Description | Example |
|---|---|---|
| .alert() | This method makes all alerts have close functionality. |
$('#identifier').alert();| onSelect | event | Close method .alert('close') | Closes all alerts. |
$('#identifier').alert('close');
|
To enable animation effects when closing, make sure you have added .fade and .in class。
Example
The following example demonstrates .alert() method usage:
Example
<
h3
Alert Plugin alert() Method
h3
<div id="myAlert" class=">alert alert-success
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Success!
strong
The result is successful.
div
<div id="myAlert2" class=">alert alert-warning
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Warning!
strong
There is a problem with your network connection.
div
<script$(function(){$(".close" )>.
click
(
function
(
)
{
$
(
"
#myAlert
"
)
.
alert
(
)
; $
(
"
#myAlert2
"
)
.
alert
(
)
;
}
)
;
}
)
;
script
Try it »
The following example demonstrates .alert('close') method usage:
Example
<
h3
Alert Plugin alert('close') Method
h3
<div id="myAlert" class=">alert alert-success
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Success!
strong
The result is successful.
div
<div id="myAlert2" class=">alert alert-warning
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Warning!
strong
There is a problem with your network connection.
div
<script$(function(){$(".close" )>.
click
(
function
(
)
{
$
(
"
#myAlert
"
)
.
alert
(
'
close
'
)
; $
(
"
#myAlert2
"
)
.
alert
(
'
close
'
)
;
}
)
;
}
)
;
script
Try it »
You can see that close functionality has been applied to all alerts, that is, closing any one alert will also close the remaining alerts.
Events
The following table lists the events used in the Alert plugin. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| close.bs.alert | When close This event is triggered immediately when the instance method is called. |
$('#myalert').bind('close.bs.alert', function () { // Do something... })| onSelect | event | closed.bs.alert | This event is triggered when the alert has been closed (it will wait for the CSS transition to finish). |
$('#myalert').bind('closed.bs.alert', function () {
// Perform some actions...
})
|
Example
The following example demonstrates the events of the Alert plugin:
Example
<div id="myAlert" class=">alert alert-success
"
<a href="" class="close" data-dismiss="alert" ×a>
<strong>Success!
strong
The result is successful.
div
<script$(function(){$("#myAlert" )>.
bind
(
'
closed.bs.alert
'
,
function
(
)
{
alert
(
"
The alert message box is closed.
"
)
;
}
)
;
}
)
;
script
Try it »
The result is as follows: