'Bootstrap dismissible alert not working
I have been working out this bug for an hour now and can't seem to find any solution to it! I'm using bootstrap and i have a dismissible alert but when i click on the x to dismiss nothing happens.
Here's my div
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Warning!</strong> Still on beta stage.
</div>
And here is my footer where i include the js files
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
Solution 1:[1]
<div class="alert alert-warning alert-dismissible" role="alert">
<span type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></span>
<strong>Warning!</strong> Still on beta stage.
</div>
Solution 2:[2]
If you are using Bootstrap 5.0 it seems in the bootstrap.js code the data-dismiss selector has changed to data-bs-dismiss.
So I just had to change data-dismiss="modal" to data-bs-dismiss="modal" and it worked.
Solution 3:[3]
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close" style="outline: none;">
<span aria-hidden="true">×</span>
</button>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
$('.alert').alert()
})
</script>
To Fix Your Problem use the code above
<script>
document.addEventListener('DOMContentLoaded', () => {
$('.alert').alert()
})
</script>
When you click on the X to dismiss the alert it runs a js function called alert() and to use this function you need to add the script-srcs given below to your code
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
Solution 4:[4]
I'm using Bootstrap v5.1 now and according to the doc the template is
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
Reference: https://getbootstrap.com/docs/5.1/components/alerts/#dismissing
Solution 5:[5]
For Bootstrap v.5.1 Just add fade show after alert-dismissible and add btn-close and data-bs-dismiss after class
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Warning!</strong> Still on beta stage.
</div>
Solution 6:[6]
Ensure that you are successfully adding the CND to the BootStrap Javascript. You may have only included the link to the CSS link when looking under the "Getting Started" section.
Add this to your header or just before your closing body.
Solution 7:[7]
try using this function
$('.close').click(function(){
$(".test").css("display", "none");
or
$('.test').hide();
});
and declare a class in span tag
<strong class="test">Warning!</strong> Still on beta stage.
should be work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Araz Shamsaddinlouy |
| Solution 2 | Hmerman6006 |
| Solution 3 | |
| Solution 4 | Brody Chen |
| Solution 5 | Jorge Montanez |
| Solution 6 | Ty Swenson |
| Solution 7 | Md.Shahjalal |
