Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap alert change class

I have created an alert in Twitter Bootstrap this way

HTML:

<div id='alert' class='hide'></div>

JS:

function showAlert(message) {
    $('#alert').html("<div class='alert alert-error'>"+message+"</div>");
    $('#alert').show();
}
showAlert('Please have a look at yourself.');
$('#alert').removeClass('alert-error');
$('#alert').addClass('alert-info');

But the last two lines of javascript don't seem to have any effects, can anyone have a look for me?

Created jsfiddle here.

Update I made some changes in my own code to make it easier to use, I prefer this way

HTML:

<div id='alert' class='hide'></div>

JS:

function showAlert(message, alertType) {
  $('#alert').html("<div class='alert alert-"+alertType+"'>"+message+"</div>");
  $('#alert').show();
}

showAlert('Please have a look at yourself.', 'success');

New jsfiddle here

like image 405
Juto Avatar asked Jan 30 '26 10:01

Juto


2 Answers

$('#alert div').removeClass('alert-error');
$('#alert div').addClass('alert-info');

http://jsfiddle.net/Cf4gs/2/

Last two javascript lines are working.

However you really wanted to do this: JSFiddle

$('.alert-error').removeClass('alert-error').addClass('alert-info');
like image 25
Martin Tale Avatar answered Feb 02 '26 03:02

Martin Tale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!