Search results

Customization in JavaScript (ES5) Message control

25 Jan 2023 / 3 minutes to read

The Message control allows the user to customize the content display positions and appearance. This section explains the details about changing the content alignments and border styles for messages.

Content Alignment

Normally, the message content is aligned to the left. The Message control allows the user to align the message content in the center or right through the built-in classes e-content-center and e-content-right.

The following example demonstrates the message with different content alignments.

Source
Preview
index.js
index.html
Copied to clipboard
var msgLeft = new ej.notifications.Message({
    content: "Your license has been activated successfully",
    severity: "Success"
  });
  msgLeft.appendTo('#msg_content_left');

  var msgCenter = new ej.notifications.Message({
    content: "The license will expire today",
    cssClass: "e-content-center",
    severity: 'Warning'
  });
  msgCenter.appendTo('#msg_content_center');

  var msgRight = new ej.notifications.Message({
    content: "The license key is invalid",
    cssClass: "e-content-right",
    severity: 'Error'
  });
  msgRight.appendTo('#msg_content_right');
Copied to clipboard
<html><head>
            
    <title>Essential JS 2 Message control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Essential JS 2 Message control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.4.38/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/20.4.38/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="msg-custom-section">
            <div class="content-section">
                <h4>Content Alignment</h4>
                <div id="msg_content_left"></div>
                <div id="msg_content_center"></div>
                <div id="msg_content_right"></div>
            </div>
        </div>
    </div>
    <style>
        /* Sample level styles */
        .msg-custom-section .content-section {
            margin: 0 auto;
            max-width: 400px;
            padding-top: 10px;
        }

        .msg-custom-section .e-message {
            margin: 10px 0;
        }
    </style>


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

Rounded and Square

To customize the Message control’s appearance, add the custom class to the message through the cssClass property. This custom class will be added to the root element. Based on this custom class, the user can override the message styles at the application level.

The following example shows the rounded and squared appearance of the message, which can be achieved by adding the cssClass property.

Source
Preview
index.js
index.html
Copied to clipboard
var msgCenter = new ej.notifications.Message({
    content: "The license will expire today",
    cssClass: "rounded",
    severity: 'Warning'
  });
  msgCenter.appendTo('#msg_rounded');

  var msgRight = new ej.notifications.Message({
    content: "The license key is invalid",
    cssClass: "square",
    severity: 'Error'
  });
  msgRight.appendTo('#msg_square');
Copied to clipboard
<html><head>
            
    <title>Essential JS 2 Message control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Essential JS 2 Message control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.4.38/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/20.4.38/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="msg-custom-section">
            <div class="content-section">
                <h4>Rounded</h4>
                <div id="msg_rounded"></div>
                <h4>Square</h4>
                <div id="msg_square"></div>
            </div>
        </div>
    </div>
    <style>
        /* Sample level styles */
        .msg-custom-section .content-section {
            margin: 0 auto;
            max-width: 400px;
            padding-top: 10px;
        }

        .msg-custom-section .e-message {
            margin: 10px 0;
        }

        .msg-custom-section .e-message.rounded {
            border-radius: 5px;
        }

        .msg-custom-section .e-message.square {
            border-radius: 1px;
        }
    </style>


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

CSS Message

The Essential JS 2 Message has predefined CSS classes that can be defined in the HTML elements, which renders the message without any script reference. This can display a simple message with content and make the code lighter.

The following DOM structure is required to display the simple message with the content.

Copied to clipboard
<div class="e-message">
    <div class="e-msg-content">..content..</div>
</div>

The following DOM structure is required to display the simple message with the content and severity icon.

Copied to clipboard
<div class="e-message">
    <span class="e-msg-icon"></span>
    <div class="e-msg-content">..content..</div>
</div>

The following is the available list of predefined CSS classes to make the appearance of a message.

Class Description
e-message Represents the message wrapper.
e-msg-icon Represents the severity type icon.
e-msg-content Represents the message content.
e-msg-close-icon Represents the close icon.
e-info Represents the information message.
e-success Represents the success message.
e-warning Represents the warning message.
e-error Represents the error message.
e-content-center Aligns the message content to the center.
e-content-right Aligns the message content to the right.

The following example shows the message which renders without any script reference.

Source
Preview
index.html
Copied to clipboard
<html><head>
            
    <title>Essential JS 2 Message control</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Essential JS 2 Message control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.4.38/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/20.4.38/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    <div id="container">
        <div class="msg-default-section">
            <div class="content-section">
                <div id="msg-default" class="e-message" role="alert">
                    <span class="e-msg-icon"></span>
                    <div class="e-msg-content">Editing is restricted</div>
                </div>
                <div id="msg-info" class="e-message e-info" role="alert">
                    <span class="e-msg-icon"></span>
                    <div class="e-msg-content">Please read the comments carefully</div>
                </div>
                <div id="msg-success" class="e-message e-success" role="alert">
                    <span class="e-msg-icon"></span>
                    <div class="e-msg-content">Your message has been sent successfully</div>
                </div>
                <div id="msg-warning" class="e-message e-warning" role="alert">
                    <span class="e-msg-icon"></span>
                    <div class="e-msg-content">There was a problem with your network connection</div>
                </div>
                <div id="msg-error" class="e-message e-error" role="alert">
                    <span class="e-msg-icon"></span>
                    <div class="e-msg-content">A problem occurred while submitting your data</div>
                </div>
            </div>
        </div>
    </div>
    <style>
        /* Sample level styles */
        .msg-default-section .content-section {
            margin: 0 auto;
            max-width: 450px;
            padding-top: 10px;
        }
    
        .msg-default-section .e-message {
            margin: 10px 0;
        }
    </style>



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