Show Bootstrap Dismissible Alert on Button Each Click using jQuery
Tags: jquery, aspnet, bootstrap5
Published: 2023-09-13 21:02:08

Show Bootstrap Dismissible Alert on Button Each Click using jQuery

Show Bootstrap Dismissible Alert on Button Each Click using jQuery
==================================================================

Alerts are a useful way to provide feedback or convey important information to users. Bootstrap provides a dismissible alert component that can be easily integrated into web applications. In this article, we will learn how to show a dismissible alert on each button click using jQuery.

Prerequisites
-------------

In order to follow along with this tutorial, you should have a basic understanding of HTML, CSS, JavaScript, jQuery, and Bootstrap.

Step 1: Include the Dependencies
--------------------------------

First, we need to include the necessary dependencies. Make sure to include the latest versions of jQuery and Bootstrap CSS and JavaScript files in your HTML document. You can either download these files and host them locally or use a CDN (Content Delivery Network) to include them.

`<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>  
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>`

Step 2: Create the HTML Markup
------------------------------

Next, let's create the HTML markup for the button and the dismissible alert:

`<button id="alertButton" class="btn btn-primary">Click me!</button>  
<div id="alertContainer"></div>`

Step 3: Write the JavaScript Code
---------------------------------

Now, we can write the JavaScript code to show the dismissible alert on each button click:

`$(document).ready(function() {  
    $("#alertButton").click(function() {  
        var alertHtml = '<div class="alert alert-success alert-dismissible fade show" role="alert">'  
            \+ 'This is a dismissible alert! Click the <button type="button" class="close" data-dismiss="alert" aria-label="Close">'  
            \+ '<span aria-hidden="true">×</span></button> button to close it.'</div>';  
        $("#alertContainer").append(alertHtml);  
    });  
});`

The code above adds a click event listener to the button with the id "alertButton". When the button is clicked, it generates the HTML markup for the dismissible alert and appends it to the div with the id "alertContainer". The alert will be displayed at the bottom of the container.

Step 4: Test the Implementation
-------------------------------

Finally, save the HTML file and open it in a web browser. Click the "Click me!" button multiple times, and you will see a new dismissible alert appearing each time you click the button.

That's it! You have successfully implemented a dismissible alert on each button click using jQuery and Bootstrap.

Conclusion
----------

In this tutorial, we learned how to show a dismissible alert on each button click using jQuery and Bootstrap. Alerts are a great way to provide important information to users, and with the help of Bootstrap, we can easily create visually appealing and user-friendly alerts.

References
----------

*   [Bootstrap Alerts Documentation](https://getbootstrap.com/docs/4.5/components/alerts/)
*   [jQuery Official Website](https://jquery.com/)

### Explore more articles on software development to enhance your skills and stay up-to-date with the latest trends in the industry.

*   #### [Integrating ChatGPT API for Health-Related Questions in Flutter: A Guide for Healthcare Chatbot Mobile App](https://devcodef1.com/news/1043700/integrating-chatgpt-api-in-flutter-for-health-related-questions)
    
    Learn how to integrate the ChatGPT API into a healthcare chatbot mobile app developed using Flutter. This guide focuses on answering health-related questions and provides step-by-step instructions.
    
*   #### [How can I multithread the creation and addition of objects to a list?](https://devcodef1.com/news/1043699/multithreading-object-creation-and-addition-to-a-list)
    
    Learn how to efficiently use multithreading in C# to create and add objects to a list, improving performance and responsiveness in your applications.
    
*   #### [How to run tomcat3 with pm2?](https://devcodef1.com/news/1043698/running-tomcat3-with-pm2)
    
    Learn how to effectively run tomcat3 with pm2 for seamless software development and deployment.
    
*   #### [Issues connecting to database on a migrated to local WordPress multisite](https://devcodef1.com/news/1043697/issues-connecting-to-database-on-a-migrated-wordpress-multisite)
    
    Encountering issues while attempting to connect to the database on a locally migrated WordPress multisite. This article provides insights and solutions to resolve the problem.
    
*   #### [App/Uninstalled Webhook Not Fired in C# Controller or NGROK](https://devcodef1.com/news/1043696/app-uninstalled-webhook-not-fired-in-c-controller-or-ngrok)
    
    Learn how to troubleshoot the issue of app/uninstalled webhook not being fired in a C# controller or NGROK setup.
    
*   #### [GCHandle Allocation in C++/CLI Application with .NET6 causing memory of variable to be corrupted (overwritten)](https://devcodef1.com/news/1043695/gchandle-allocation-in-c-cli-application-with-net6)
    
    Learn about the issue of memory corruption in C++/CLI applications with .NET6 caused by GCHandle allocation and how to mitigate it.
    
*   #### [Resolving the getid Error in Google App Scripts](https://devcodef1.com/news/1043694/resolving-the-getid-error-in-google-app-scripts)
    
    Learn how to resolve the getid error in Google App Scripts and ensure smooth execution of your scripts.