function addNotification(text='Sample notification', type=4) { var container = document.querySelector('.notifications'); // Create notification element var div = document.createElement('div'); div.classList.add('sniffle__notification'); div.onclick = function() { if (div.parentNode) { div.classList.add('sniffle__notification--hide'); setTimeout(function() { container.removeChild(div); }, 500); } }; // Create icon element and append to notification var icon = document.createElement('span'); icon.classList.add('sniffle__notification-icon'); switch (type) { case 1: div.classList.add('sniffle__notification--success'); icon.innerHTML = '\ \ '; break; case 2: div.classList.add('sniffle__notification--error'); icon.innerHTML = '\ \ '; break; case 3: div.classList.add('sniffle__notification--warning'); icon.innerHTML = '\ \ '; break; default: div.classList.add('sniffle__notification--info'); icon.innerHTML = '\ \ '; break; } div.appendChild(icon); // Create text element and append to notification var description = document.createElement('span'); description.classList.add('sniffle__notification-text'); description.innerHTML = text; div.appendChild(description); // Create span to show time remaining var timer = document.createElement('span'); timer.classList.add('sniffle__notification-time'); div.appendChild(timer); // Append notification to container container.appendChild(div); setTimeout(function() { div.classList.add('sniffle__notification-show'); }, 100); // Remove notification after 5 seconds setTimeout(function() { if (div.parentNode) { div.classList.add('sniffle__notification--hide'); setTimeout(function() { container.removeChild(div); }, 500); } }, 5000); }