Hello folks! welcome back to a new section of our tutorial on Bootstrap. In this section of our tutorial on Bootstrap, we will discuss about the Bootstrap Alert Plugin.
Alerts are often used to display information such as warning or confirmation messages to the end users. Making use of alert plugin, you can add dismiss functionality to all the alert messages.
Alerts are often used to display information such as warning or confirmation messages to the end users. Making use of alert plugin, you can add dismiss functionality to all the alert messages.
If you want to add this bootstrap plugin functionality individually, you are going to need alert.js. Else, as mentioned in the tutorial Bootstrap plugins overview, you can add bootstrap.js or the manified bootstrap.min.js.
Usage
You can enable dismissal of an alert in the following two ways below -
- Via data-attributes - To dismiss using data attributes, just add data-dismiss = "alert" to your close button in order to automatically give an alert a close functionality.
<a class = "close" data-dismiss = "alert" href = "#" aria-hidden = "true"> × </a>
- Via JavaScript - To dismiss using JavaScript, use the following code below -
$(".alert").alert()
Example
Following example demonstrates the usage of alert plugin via data attributes -
<div class = "alert alert-success"> <a href = "#" class = "close" data-dismiss = "alert"> × </a> <strong>Warning!</strong> There was a problem with your network connection. </div>
Output
When the above example is executed, it will produce the following result -
×Warning! There was a problem with your network connection.
Options
There are no options.
Methods
The following table below lists some useful methods for alert plugin -
Method | Description | Example |
---|---|---|
.alert() | This method wraps all alerts with close functionality. | $('#identifier').alert(); |
Close Method .alert('close') | To enable all alerts to be closed, add this method. | $('#identifier').alert('close'); |
Example
Following example demonstrates the usage of .alert() method -
<h3>Alert messages to demonstrate 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 = "myAlert" class = "alert alert-warning"> <a href = "#" class = "close" data-dismiss = "alert">×</a> <strong>Warning!</strong> There was a problem with your network connection. </div> <script type = "text/javascript"> $(function(){ $(".close").click(function(){ $("#myAlert").alert(); }); }); </script>
Output
When the above example is executed, it will produce the following result -
Alert messages to demonstrate alert() method
×Success! the result is successful.×Warning! There was a problem with your network connection.
Example
Following example demonstrates the usage of .alert('close') method -
<h3>Alert messages to demonstrate 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 = "myAlert" class = "alert alert-warning"> <a href = "#" class = "close" data-dismiss = "alert">×</a> <strong>Warning!</strong> There was a problem with your network connection. </div> <script type = "text/javascript"> $(function(){ $(".close").click(function(){ $("#myAlert").alert('close'); }); }); </script>
Output
When the above example is executed, it will produce the following result -
Alert messages to demonstrate alert('close') method
×Success! the result is successful.×Warning! There was a problem with your network connection.
Events
Following table lists the events to work with alert plugin. This event may be used to hook into the alert function -
Event | Description | Example |
---|---|---|
close.bs.alert | This event fires immediately when the close instance method is called. | $('#myalert').bind('close.bs.alert', function () { // do something }) |
closed.bs.alert | This event is fired when the alert has been closed (will wait for CSS transitions to complete). | $('#myalert').bind('closed.bs.alert', function () { // do something }) |
Example
Following example demonstrates the usage of the alert plugin events -
<div id = "myAlert" class = "alert alert-success"> <a href = "#" class = "close" data-dismiss = "alert">×</a> <strong>Success!</strong> the result is successful. </div> <script type = "text/javascript"> $(function(){ $("#myAlert").bind('closed.bs.alert', function () { alert("Alert message box is closed."); }); }); </script>
Output
When the above example is executed, it will produce the following result -
×Success! the result is successful.
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial guide, we are going to be studying about the Bootstrap Button Plugin.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.