I'm using Bootstrap 2.0 for my project and I would like to dynamically add Bootstrap alert box in my page (http://twitter.github.com/bootstrap/javascript.html#alerts). I want to do something like:
bootstrap-alert.warning("Invalid Credentials");
The standard alert box in JavaScript does not provide the option to apply CSS. To style your alert box, you need to create a custom one first. The custom alert box will be created using jQuery and styles will be applied to CSS.
One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen.
JavaScript Popup Boxes: Summary If you want to display an alert of some kind to a user, use alert() . If you want to display a confirmation box with option buttons, use confirm() . If you want to use a pop up with an input and option buttons, which can display inputed values, use prompt() .
Try this (see a working example of this code in jsfiddle: http://jsfiddle.net/periklis/7ATLS/1/)
<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>
EDIT: There are now libraries that simplify and streamline this process, such as bootbox.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With