Config in EJ2 JavaScript Toast control

10 May 202315 minutes to read

This section explains the steps required to customize the appearance of the toast using built-in APIs.

Title and content template

Toast can be created with the notification message. The message contains title and content of the toasts. The title and contents are adaptable in any resolution.

The Title or Content property can be given as HTML element/element ID to a string that can be displayed as a toast.

ej.base.enableRipple(true);

//Initialize Toast component
var toast = new ej.notifications.Toast({
    title: 'Matt sent you a friend request',
    content: 'You have a new friend request yet to accept',
    position: { X: "Center" }
});
//Render initialized Toast component
toast.appendTo('#element');
toast.show();

document.getElementById('show_toast').onclick = function () {
    toast.show();
}
<!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 class="row"> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id="element"></div>
        <br><br>
        <div id="result"></div>
        <div id="templateToast" style="display: none;color:red"> System affected by virus !!! </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>

Specifying custom target

By default, the toast can be rendered in the document body. You can change the target position for toast rendering using the target property. Based on the target, the position will be updated.

Close button

By default, the showCloseButton is not enabled. You can enable it by setting the true value. Before expiring the toast, you can use this button to close or destroy toasts manually.

Progress bar

By default, the showProgressBar is not enabled. If it is enabled, it can visually indicate when will the toast gets expired. Based on the timeOut property, progress bar will appear.

Progress bar direction

By default, the progressDirection is set to “Rtl” and it will appear from right to left direction. You can change the progressDirection to “Ltr” to make it appear from left to right direction.

Newest on top

By default, the newly created toasts will append next with existing toasts. You can change the sequence like inserting before the toast by enabling the newestOnTop.

Here, The following sample demonstrates the combination of the target, showCloseButton, showProgressBar, and newestOnTop properties in toast.

ej.base.enableRipple(true);

//Initialize Toast component
var toast = new ej.notifications.Toast({
    title: 'File Downloading',
    content: '<div class="progress"><span style="width: 80%"></span></div>',
    showCloseButton: true,
    target: '#toast_target',
    position: { X: "Center" },
    newestOnTop: true,
    showProgressBar: true
});
//Render initialized Toast component
toast.appendTo('#element');
toast.show();

document.getElementById('show_toast').onclick = function () {
  toast.show();
};
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
	<style>
	  #toast_target {
	    height: 200px;
	  }
	</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id='element'></div>
        <br/><br/>
        <div id='toast_target'></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>

Width and height

The dimensions of the toast can be set using the width and height properties. This will individually set all toasts. You can create different custom dimension toasts.

By default, the toast can be rendered with 300px width with auto height.

In mobile devices, the default width of the toast gets ‘100%’ width of the page.
When you set toast width as ‘100%’, the toast occupies full width and will be displayed at the top or bottom based on the position Y property.

Both the width and height properties allow setting pixels/numbers/percentage. The number value is considered as pixels.

ej.base.enableRipple(true);

//Initialize Toast component
var toast = new ej.notifications.Toast({
    title: 'Matt sent you a friend request',
    content: 'You have a new friend request yet to accept',
    position: { X: "Center", Y: "Bottom" },
    width: 400,
    height: 150,
});

var radioButton = new ej.buttons.RadioButton({ label: 'Top', name: 'toast', value: 'Target', change: checkboxChange });
radioButton.appendTo('#topAlign');

var radioButton2 = new ej.buttons.RadioButton({ label: 'Bottom', name: 'toast', value: 'Global', checked: true, change: checkboxChange1 });
radioButton2.appendTo('#bottomAlign');

var checkBoxObj = new ej.buttons.CheckBox({ label: '100% Width', change: onChange });
checkBoxObj.appendTo('#fullWidth');
toast.appendTo('#element');

let timeDelay = 500;

function onChange(e) {
  if (e.checked) {
    toast.hide();
    toast.width = "100%";
    toast.title="";
    toast.content="<div class='e-custom'>Take a look at our next generation <b>Javascript</b> <a href='https://ej2.syncfusion.com/home/index.html' target='_blank'>LEARN MORE</a></div>";
    toastShow(timeDelay);
  } else {
    toast.hide();
    toast.width= 300;
    toast.title= 'Matt sent you a friend request',
    toast.content='You have a new friend request yet to accept',
    toastShow(timeDelay);
  }
}

function toastShow(timeOutDelay) {
    setTimeout(
        function () {
            toast.show();
        }, timeOutDelay);
}

function checkboxChange(e) {
    if (e.event.target.checked) {
      toast.position.Y = "Top";
      toast.hide();
      toastShow(timeDelay);
    }
}

function checkboxChange1(e) {
    if (e.event.target.checked) {
      toast.position.Y = "Bottom";
      toast.hide();
      toastShow(timeDelay);
    }
}

document.getElementById('show_toast').onclick = function () {
    toast.show();
};
<!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="https://cdn.syncfusion.com/ej2/25.1.35/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div id='element'></div>
            <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
            <div class='row' style="padding-top: 20px" id= "toast_pos_target">
              <table>
                <tr>
                  <td>
                      <div style='padding:25px 0 0 0;'>
                          <input id="topAlign" type="radio">
                      </div>
                  </td>
                  <td>
                      <div style='padding:25px 0 0 0;'>
                          <input id="bottomAlign" type="radio">
                      </div>
                  </td>
                </tr>
                <tr>
                   <td>
                     <div style='padding:25px 0 0 0;'>
                          <input id="fullWidth" type="checkbox">
                      </div>
                   </td>
                </tr>
              </table>
            </div>
        <br/><br/>
        <div id='result'></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>

See Also