Building a Custom JavaScript Toast Notification System: A Step-by-Step Guide

PHOTO EMBED

Sat Apr 01 2023 14:05:01 GMT+0000 (Coordinated Universal Time)

Saved by @Jaimin047 #javascript #toastnotification #notification

function showToast(message, duration) {
  // Create a new div element for the toast message
  const toast = document.createElement('div');
  toast.classList.add('toast');

  // Add the message content to the toast
  toast.innerHTML = message;

  // Add the toast to the container element
  const container = document.getElementById('toast-container');
  container.appendChild(toast);

  // Use a timer to remove the toast after the specified duration
  setTimeout(() => {
    container.removeChild(toast);
  }, duration);
}
content_copyCOPY