Reputation:
Im using the following code to add message and its working fine,the problem is that when I put the second message I see it on top of the preivos message and so on... there is a way to remove the previos message from the UI when I add new one
function addInfoMessage(message) {
var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>×</button></div>");
$messages.append($msg);
}
Upvotes: 0
Views: 101
Reputation: 9637
Use in empty() in jQuery to remove all children element
$messages.empty();
var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>×</button></div>");
$messages.append($msg);
Upvotes: 2
Reputation: 207511
Do not append, just replace the content with html()
$messages.html($msg);
Upvotes: 4
Reputation: 28513
You can use .html()
instead of .append()
, this will replace all html code inside $messages
function addInfoMessage(message) {
var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>×</button></div>");
$messages.html($msg);
}
Upvotes: 2
Reputation: 2969
you have to select the message and remove it, like this:
function addInfoMessage(message) {
$messages.find('.alert.alert-info').remove();
var $msg = $("<div class='alert alert-info'><strong>Information : </strong>" + message + "<button type='button' class='close' aria-hidden='true'>×</button></div>");
$messages.append($msg);
}
Upvotes: 2