HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

How To Create a Toast

To create a toast, use the .toast class, and add a .toast-header and a .toast-body inside of it.

Example

<div class="toast show">
  <div class="toast-header">
    Toast Header
    <button type="button" class="btn-close" data-bs-dismiss="toast"></button>
  </div>
  <div class="toast-body">
    Some text inside the toast body
  </div>
</div>
You can click on above box to edit the code and run again.

Output

Open a Toast

To show a toast with a click of a button, you must initialize it with JavaScript: select the specified element and call the toast() method.

Example

<script>
document.getElementById("toastbtn").onclick = function() {
  var toastElList = [].slice.call(document.querySelectorAll('.toast'))
  var toastList = toastElList.map(function(toastEl) {
    return new bootstrap.Toast(toastEl)
  })
  toastList.forEach(toast => toast.show())
}
</script>
You can click on above box to edit the code and run again.

Output