Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle a bootstrap alert on and off with button click?

I want to display an alert box multiple times with a button click event without having to refresh the page but the alert box only shows up once. If I want to show the alert box again, I have to refresh page.

Currently if you render a bootstrap alert on the page, when you close it, the div container of the alert is gone from the page. So when you click the button again, it doesn't show up anymore. I have been doing the following on the close button to make it hide instead of delete the div container of alert.

<button class="close" onclick="$('#alertBox').hide();; return false;">×</button> 

I wonder if using data-toggle or data-dismiss in bootstrap can make this sort of toggle effect working without going through my own custom Javascript handlers.

like image 926
ttback Avatar asked Jun 05 '12 19:06

ttback


People also ask

How do I make Bootstrap alerts automatically disappear?

To close the alert message, add a . alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).

How do I show and hide a Bootstrap alert?

show() and $(". alert"). hide() will show/hide all the elements of the . alert class.

How do I toggle in Bootstrap?

Refer to Bootstrap Form Controls documentation to create inline checkboxes. Simply add data-toggle="toggle" to a convert checkboxes into toggles.

How can show hide div on button click in Bootstrap?

To hide div by default and show it on click with Bootstrap, we add the data-toggle attribute. to add an a element and a button that has the data-toggle attribyte set to collapse . We also set the href attribute to the IDs of the divs.


1 Answers

I've had the same problem: just don't use the data-dismiss from the close button and work with JQuery show() and hide():

$('.close').click(function() {    $('.alert').hide(); }) 

Now you can show the alert when clicking a button by using the code:

$('.alert').show() 

Hope this helps!

like image 92
Ron Avatar answered Oct 02 '22 18:10

Ron