Es5 getting started in EJ2 JavaScript Menu

24 Nov 202611 minutes to read

The Essential® JS 2 for JavaScript (global script) is an ES5 formatted pure JavaScript framework which can be directly used in latest web browsers.

Dependencies

The following list of dependencies are required to use the Menu control in your application.

|-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Create a Folder and HTML File

  • Create a folder named quickstart in your desired directory.

  • Inside the quickstart folder, create a new file named index.html.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts

Add Syncfusion® Menu CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base, ej2-data, ej2-lists, ej2-inputs, ej2-buttons, and ej2-popups scripts are required dependency packages for the Menu component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/menu/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-data/dist/global/ej2-data.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/32.1.37/ej2-lists/dist/global/ej2-lists.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-navigations/dist/global/ej2-navigations.min.js

Adding Menu component to the application

Add the HTML UL tag with the id attribute as menu to your index.html file.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
                
<title>Essential JS 2 - Menu</title>

<!-- Essential JS 2 Menu's dependent bootstrap5.3 theme -->
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/menu/index.css" rel="stylesheet" />

<!-- Essential JS 2 Menu's dependent scripts -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>

<!-- Essential JS 2 Menu's global script -->
<script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-navigations/dist/global/ej2-navigations.min.js" type="text/javascript"></script>

<!--style reference from app-->
<link href="styles.css" rel="stylesheet" />
</head>

<body>
<!--element which is going to render-->
<ul id="menu"></ul>

<script>
    ej.base.enableRipple(true);

    //Menu items definition
    var menuItems = [
        {
            text: 'File',
            items: [
                { text: 'Open' },
                { text: 'Save' },
                { text: 'Exit' }
            ]
        },
        {
            text: 'Edit',
            items: [
                { text: 'Cut' },
                { text: 'Copy' },
                { text: 'Paste' }
            ]
        },
        {
            text: 'View',
            items: [
                { text: 'Toolbar' },
                { text: 'Sidebar' }
            ]
        },
        {
            text: 'Tools',
            items: [
                { text: 'Spelling & Grammar' },
                { text: 'Customize' },
                { text: 'Options' }
            ]
        },
        { text: 'Go' },
        { text: 'Help' }
    ];

    // Initialize Menu control.
    var menuObj = new ej.navigations.Menu({ items: menuItems }, '#menu');
</script>
</body>

</html>

Group menu items with Separator

The separators are both horizontal and vertical lines used to separate the menu items. You cannot select the separators, but you can enable separators to group the menu items using the separator property. The Open and Save sub menu items are grouped using the separator property in the following sample.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Essential JS 2 - Menu</title>

    <!-- Essential JS 2 Menu's dependent bootstrap5.3 theme -->
    <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/menu/index.css" rel="stylesheet" />

    <!-- Essential JS 2 Menu's dependent scripts -->
    <script src="https://cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
    <script src="https://cdn.syncfusion.com/ej2/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
    <script src="https://cdn.syncfusion.com/ej2/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
    <script src="https://cdn.syncfusion.com/ej2/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>

    <!-- Essential JS 2 Menu's global script -->
    <script src="https://cdn.syncfusion.com/ej2/ej2-navigations/dist/global/ej2-navigations.min.js" type="text/javascript"></script>

    <!--style reference from app-->
    <link href="styles.css" rel="stylesheet" />
</head>

<body>
    <!--element which is going to render-->
    <ul id="menu"></ul>

    <script>
        ej.base.enableRipple(true);
        //Menu items definition
        var menuItems = [
            {
                text: 'File',
                items: [
                    { text: 'Open' },
                    { text: 'Save' },
                    { separator: true },
                    { text: 'Exit' }
                ]
            },
            {
                text: 'Edit',
                items: [
                    { text: 'Cut' },
                    { text: 'Copy' },
                    { text: 'Paste' }
                ]
            },
            {
                text: 'View',
                items: [
                    { text: 'Toolbar' },
                    { text: 'Sidebar' },
                    { text: 'Full Screen' }
                ]
            },
            {
                text: 'Tools',
                items: [
                    { text: 'Spelling & Grammar' },
                    { text: 'Customize' },
                    { text: 'Options' }
                ]
            },
            { text: 'Go' },
            { text: 'Help' }
        ];

        // Initialize Menu control.
        var menuObj = new ej.navigations.Menu({ items: menuItems }, '#menu');
    </script>
</body>

</html>

The separator property should not be given along with the other fields in the MenuItem. You can also enable the separator to group horizontal menu items.