最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter Bootstrap alert (notification) dynamically change text - Stack Overflow

programmeradmin6浏览0评论

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 plicated? Really?

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 plicated? Really?

Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Aug 7, 2013 at 13:24 JutoJuto 1,2761 gold badge13 silver badges24 bronze badges 2
  • 3 The solution is about 5 lines of Javascript. I don't think you can reasonably call that plicated. – glosrob Commented Aug 7, 2013 at 13:27
  • Possible duplicate of Dynamically create Bootstrap alerts box through JavaScript – Mahdi Alkhatib Commented Jul 26, 2017 at 7:47
Add a ment  | 

3 Answers 3

Reset to default 8

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

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.

<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>​
发布评论

评论列表(0)

  1. 暂无评论