Let's say I've the following piece of code:
<div class="alert alert-info fade in" id="bsalert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Info!</strong> This alert box could indicate a neutral informative or action
</div>
How can I activate it manualy by using jQuery?
Bootstrap uses the in
and out
class for visibility. You just need to toggle those classes. Also, if you want to keep the alert around once cancelled you can add a return false to the close.bs.alert
event.
function toggleAlert(){
$(".alert").toggleClass('in out');
return false; // Keep close.bs.alert event from removing from DOM
}
$("#btn").on("click", toggleAlert);
$('#bsalert').on('close.bs.alert', toggleAlert)
<link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<button id="btn">Toggle</button>
<div class="alert alert-info fade out" id="bsalert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Info!</strong> This alert box could indicate a neutral informative or action
</div>
<div>
content
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