Toast services in EJ2 JavaScript Toast control

10 May 20238 minutes to read

The Toast component provides a built-in utility function to render the toast with minimal code. The utility function will render the toast without the need of rendering the container element in the DOM where the toast is appended. So that, the toast can now be rendered on the go. The following are the option to render the toast using the utility function.

Show Toast with predefined types

The Toast component support 4 types of predefined toast with essential colors for various situations which can be shown using the ToastUtility.show by just defining the type of the toast without defining any class names. The following options are used as an argument on calling the utility function for predefined types:

Options Description
content Specifies the content that can be displayed on the Toast.
type Specifies the type of the predefined Toasts. The 4 types of predefined toasts are Information, Success, Error, Warning
timeOut Specifies the Toast display time duration on the page in milliseconds. Once the time expires, Toast message will be removed. Setting 0 as a time out value displays the Toast on the page until the user closes it manually.
//Initialize Toast component using utility
var toastObj;
document.getElementById('info_Toast').onclick = function () {
    toastObj = ej.notifications.ToastUtility.show('Please read the comments carefully', 'Information', 20000);
};
document.getElementById('success_Toast').onclick = function () {
    toastObj = ej.notifications.ToastUtility.show('Your message has been sent successfully', 'Success', 20000);
};
document.getElementById('error_Toast').onclick = function () {
    toastObj = ej.notifications.ToastUtility.show('A problem has been occurred while submitting the data', 'Error', 20000);
};
document.getElementById('warning_Toast').onclick = function () {
    toastObj = ej.notifications.ToastUtility.show('There was a problem with your network connection', 'Warning', 20000);
};
document.getElementById('hide_Toast').onclick = function () {
   toastObj.hide('All');
};
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Toolbar Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div>
            <button class="e-btn e-control e-info" id="info_Toast"> Info Message </button>
            <button class="e-btn e-control e-success" id="success_Toast"> Success Message </button>
            <button class="e-btn e-control e-warning" id="warning_Toast"> Warning Message </button>
            <button class="e-btn e-control e-danger" id="error_Toast"> Danger Message </button>
        </div>
        <br>
        <div style="text-align: center;">
            <button class="e-btn e-control" id="hide_Toast"> Hide All </button>
        </div>
    </div>

<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Show Toast with ToastModel

The utility function can be called using the ToastModel as argument to show the toast where all the properties in the ToastModel like any events, position, close icon, action buttons, etc. can be used in the ToastUtility.show.

//Initialize Toast component using utility function with ToastModel
var toastObj;
document.getElementById('show_Toast').onclick = (): void => {
    toastObj = ej.notifications.ToastUtility.show({
        title: 'Toast Title',
        content: 'Toast shown using utility function with ToastModel',
        timeOut: 20000,
        position: { X: 'Right', Y: 'Bottom' },
        showCloseButton: true,
        click: toastClick,
        buttons:  [{
            model: { content: 'Close' }, click: toastClose
        }]
    })
};

function toastClick() {
    console.log('Toast click event triggered');
}

function toastClose() {
    toastObj.hide();
}
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Toolbar Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div style="text-align: center;">
            <button class="e-btn e-control" id="show_Toast">Show Toast</button>
        </div>
    </div>

<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>