I'm trying to dynamically create alert messages using the jQuery plugin for Bootstrap CSS. I want to create and destroy alerts on certain events (e.g. AJAX success/error). Here's a non-working snippet of my code:
var alertVisible = false;
function fetchData() {
function onDataReceived(stats) {
if (alertVisible) {
$(.alert-message).alert(close);
}
alertVisible = false;
// Do stuff with data...
}
function onError() {
$(.alert-message).alert();
alertVisible = true;
}
$.ajax({
dataType: 'json',
success: onDataReceived,
error: onError,
cache: false
});
};
and here's the corresponding HTML:
<div class=row>
<div class=alert-message error fade in hide span16 data-alert=alert>
<a class=close href=#>×</a>
<p>Lost connection to server.</p>
</div>
</div>
My first problem is that the alert is showed by default. I can kinda solve this by using the hide
class. However, if you close an alert (by clicking on the close button) then creating new asserts no longer works (I guess the DOM element is gone). How are you supposed to use Bootstrap alerts?