Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create Bootstrap alerts box through JavaScript

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");  
like image 859
ed1t Avatar asked Apr 10 '12 02:04

ed1t


People also ask

Can we customize alert box in JavaScript?

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.

Which JavaScript method is used to write into an alert box?

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.

How do you create an alert popup in JavaScript?

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() .


1 Answers

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

like image 58
periklis Avatar answered Oct 02 '22 11:10

periklis