Juto
Juto

Reputation: 1276

Twitter Bootstrap alert (notification) dynamically change text

Simple question, but I didn't succeed finding a satisfactory answer on SO.

When I have a bootstrap alert:

<div id='alert' class='alert alert-error hide'>Alert.</div>

I want the text to be controlled using either Javascript of by jQuery, I just looked at this solution to this problem, but does it have to be so complicated? Really?

Upvotes: 3

Views: 13108

Answers (3)

Yahya Yahyaoui
Yahya Yahyaoui

Reputation: 2953

As simple as: $("#alert").text("My new Text");

Upvotes: 8

Gathole
Gathole

Reputation: 932

<input type = "button" id = "clickme" value="Click me!"/>
<div id = "alert_placeholder"></div>
<script>
bootstrap_alert = function() {}
bootstrap_alert.warning = function(message) {
            $('#alert_placeholder').html('<div class="alert"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
        }

$('#clickme').on('click', function() {
            bootstrap_alert.warning('Your text goes here');
});
</script>​

Upvotes: 0

Juto
Juto

Reputation: 1276

Referenced the solution and now came up with this:

HTML:

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

Javascript:

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

Working jsfiddle.

Upvotes: 7

Related Questions