Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Calling Bootstrap Toast Methods via JavaScript</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script> document.addEventListener("DOMContentLoaded", function(){ var showBtn = document.getElementById("showBtn"); var hideBtn = document.getElementById("hideBtn"); var disposeBtn = document.getElementById("disposeBtn"); // Create toast instance var myToast = new bootstrap.Toast(document.getElementById("myToast")); showBtn.addEventListener("click", function(){ myToast.show(); }); hideBtn.addEventListener("click", function(){ myToast.hide(); }); disposeBtn.addEventListener("click", function(){ myToast.dispose(); }); }); </script> </head> <body> <div class="m-4"> <h3>Toast Methods</h3> <p>Click on the buttons to manually show/hide the toast.</p> <button type="button" class="btn btn-primary" id="showBtn">Show Toast</button> <button type="button" class="btn btn-warning" id="hideBtn">Hide Toast</button> <button type="button" class="btn btn-danger" id="disposeBtn">Dispose Toast</button> <div class="toast" id="myToast" data-bs-autohide="false" style="position: absolute; top: 10px; right: 10px;"> <div class="toast-header"> <strong class="me-auto"><i class="bi-gift-fill"></i> We miss you!</strong> <small>10 mins ago</small> <button type="button" class="btn-close" data-bs-dismiss="toast"></button> </div> <div class="toast-body"> It's been a long time since you visited us. We've something special for you. <a href="#">Click here!</a> </div> </div> </div> </body> </html>