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

jqueryjquery-uitwitter-bootstrap

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.

Best Answer

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!