Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Get Bootstrap Toast Instance Using jQuery</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://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <style> /* Style for positioning toast */ .toast{ position: absolute; bottom: 10px; right: 10px; } </style> <script> document.addEventListener("DOMContentLoaded", function(){ var btn = document.getElementById("myBtn"); var element = document.getElementById("myToast"); // Create toast instance var bsToast = new bootstrap.Toast(element); // Show toast on page load bsToast.show(); // Get toast instance on button click btn.addEventListener("click", function(){ var myToast = bootstrap.Toast.getInstance(element); console.log(myToast); }); }); </script> </head> <body> <div class="m-4"> <p class="mt-4"><strong>Note:</strong> Press the "Get Toast Instance" button to get the toast instance associated with a DOM element.</p> <p>Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac) to open the browser console panel.</p> <button type="button" class="btn btn-primary" id="myBtn">Get Toast Instance</button> <div class="toast" id="myToast" data-bs-autohide="false"> <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>