How to Automatically Close Alerts using Twitter Bootstrap ?
Last Updated :
01 Jun, 2020
All modern web application uses alerts to show messages to the user. These alerts have a close button to close them or can be closed automatically using Twitter Bootstrap. In this post, we will create automatically closing of alerts using twitter Bootstrap.
Basic idea is to use jQuery setTimeout() method to close the alert after specified time.
Syntax:
setTimeout(function, delay)
Note: Delay is defined in milliseconds. 1 second is equal to 1000 milliseconds.
Example 1: In this example, alert is accessed in script using the id and then alert is closed after 5 seconds.
HTML
<!DOCTYPE html>
< html >
< head >
< title >
How to Automatically Close Alerts
using Twitter Bootstrap ?
</ title >
< link rel = "stylesheet" href =
< script src =
</ script >
< script src =
</ script >
</ head >
< body >
< h3 >Automatically closing the alert</ h3 >
< div id = "alert" class = "alert alert-danger" >
This alert will automatically
close in 5 seconds.
</ div >
< script type = "text/javascript" >
setTimeout(function () {
// Closing the alert
$('#alert').alert('close');
}, 5000);
</ script >
</ body >
</ html >
|
Example 2: In this example, alert is accessed in script using it’s class and alert closes after 2 seconds.
HTML
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "content-type"
content = "text/html;charset=utf-8" />
< title >
How to Automatically Close Alerts
using Twitter Bootstrap ?
</ title >
< link rel = "stylesheet" href =
< script src =
</ script >
< script src =
</ script >
</ head >
< body >
< h3 >Automatically closing the alert</ h3 >
< div class = "alert alert-danger" >
This alert will automatically
close in 2 seconds.
</ div >
< script type = "text/javascript" >
setTimeout(function () {
// Closing the alert
$('.alert').alert('close');
}, 2000);
</ script >
</ body >
</ html >
|
Example 3: In this example, bootstrap hide class is dynamically added to alert to close the alert after 5 seconds. The addClass() is the method that is used to add class to any element using JavaScript.
HTML
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "content-type"
content = "text/html;charset=utf-8" />
< title >
How to Automatically Close Alerts
using Twitter Bootstrap ?
</ title >
< link rel = "stylesheet" href =
< script src =
</ script >
< script src =
</ script >
</ head >
< body >
< h3 >Automatically closing the alert</ h3 >
< div id = "alert" class = "alert alert-danger" >
This alert will automatically
close in 5 seconds.
</ div >
< script type = "text/javascript" >
setTimeout(function () {
// Adding the class dynamically
$('#alert').addClass('hide');
}, 5000);
</ script >
</ body >
</ html >
|
Output:
Share your thoughts in the comments
Please Login to comment...