2014-07-12 11:20:15 +02:00
---
layout: page
title: Alerts
---
2014-07-13 09:53:59 +02:00
Add dismiss functionality to all alert messages with this plugin.
## Examples
{% example html %}
< div class = "alert alert-warning alert-dismissible fade in" role = "alert" >
< button type = "button" class = "close" data-dismiss = "alert" >
< span aria-hidden = "true" > × < / span >
< span class = "sr-only" > Close< / span >
< / button >
< strong > Holy guacamole!< / strong > Best check yo self, you're not looking too good.
< / div >
{% endexample %}
2014-12-01 05:17:45 +01:00
When using a `.close` button, it must be the first child of the `.alert-dismissible` and no text content may come before it in the markup.
2014-07-13 09:53:59 +02:00
{% example html %}
< div class = "alert alert-danger alert-dismissible fade in" role = "alert" >
< button type = "button" class = "close" data-dismiss = "alert" > < span aria-hidden = "true" > × < / span > < span class = "sr-only" > Close< / span > < / button >
< h4 > Oh snap! You got an error!< / h4 >
< p > Change this and that and try again. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.< / p >
< p >
< button type = "button" class = "btn btn-danger" > Take this action< / button >
< button type = "button" class = "btn btn-secondary" > Or do this< / button >
< / p >
< / div >
{% endexample %}
## Usage
Enable dismissal of an alert via JavaScript:
{% highlight js %}
$(".alert").alert()
{% endhighlight %}
### Markup
2014-09-18 00:21:31 +02:00
Just add `data-dismiss="alert"` to your close button to automatically give an alert close functionality. Closing an alert removes it from the DOM.
2014-07-13 09:53:59 +02:00
{% highlight html %}
< button type = "button" class = "close" data-dismiss = "alert" >
< span aria-hidden = "true" > × < / span >
< span class = "sr-only" > Close< / span >
< / button >
{% endhighlight %}
2014-09-18 00:21:31 +02:00
To have your alerts use animation when closing, make sure they have the `.fade` and `.in` classes already applied to them.
2014-08-03 03:30:59 +02:00
2014-07-13 09:53:59 +02:00
### Methods
#### $().alert()
2014-08-03 03:30:59 +02:00
Makes an alert listen for click events on descendant elements which have the `data-dismiss="alert"` attribute. (Not necessary when using the data-api's auto-initialization.)
2014-07-13 09:53:59 +02:00
2014-08-03 03:30:59 +02:00
#### $().alert('close')
2014-07-13 09:53:59 +02:00
2014-09-18 00:21:31 +02:00
Closes an alert by removing it from the DOM. If the `.fade` and `.in` classes are present on the element, the alert will fade out before it is removed.
2014-07-13 09:53:59 +02:00
{% highlight js %}$(".alert").alert('close'){% endhighlight %}
### Events
2014-09-18 00:21:31 +02:00
Bootstrap's alert plugin exposes a few events for hooking into alert functionality.
2014-07-13 09:53:59 +02:00
< div class = "table-responsive" >
< table class = "table table-bordered table-striped" >
< thead >
< tr >
< th style = "width: 150px;" > Event Type< / th >
< th > Description< / th >
< / tr >
< / thead >
< tbody >
< tr >
< td > close.bs.alert< / td >
< td > This event fires immediately when the < code > close< / code > instance method is called.< / td >
< / tr >
< tr >
< td > closed.bs.alert< / td >
< td > This event is fired when the alert has been closed (will wait for CSS transitions to complete).< / td >
< / tr >
< / tbody >
< / table >
< / div >
2014-03-17 15:58:03 +01:00
{% highlight js %}
2014-08-03 03:30:59 +02:00
$('#myAlert').on('closed.bs.alert', function () {
2014-03-17 15:58:03 +01:00
// do something…
})
{% endhighlight %}