Es5 getting started in EJ2 JavaScript Context menu

24 Nov 20268 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.

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® ContextMenu 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 ContextMenu component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/context-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-inputs/dist/global/ej2-inputs.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-navigations/dist/global/ej2-navigations.min.js

Adding ContextMenu component to the application

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

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>
        <!-- Essential JS 2 ContextMenu's dependent bootstrap5.3 theme -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/context-menu/index.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />

        <!-- Essential JS 2 ContextMenu's dependent global script -->
        <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-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 ContextMenu's control global script -->
        <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-navigations/dist/global/ej2-navigations.min.js" type="text/javascript"></script>
   </head>
   <body>
        <!--target element-->
        <div id="target">Right click / Touch hold to open the ContextMenu</div>
        <!--element which is going to render-->
        <ul id="contextmenu"></ul>
        <script>
            ej.base.enableRipple(true);

            var menuItems = [
            {
              text: 'Cut'
            },
            {
              text: 'Copy'
            },
            {
              text: 'Paste'
            }];
            var menuOptions = {
              target: '#target',
              items: menuItems
            };

            // Initialize ContextMenu control.
            var menuObj = new ej.navigations.ContextMenu(menuOptions, '#contextmenu');
        </script>
   </body>
  </html>

Rendering items with Separator

The Separators are the horizontal lines that are used to separate the menu items. You cannot select the separators. You can enable separators to group the menu items using the separator property. Cut, Copy, and Paste 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</title>
        <!-- Essential JS 2 bootstrap5.3 theme -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/context-menu/index.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />

        <!-- Essential JS 2 ContextMenu's global script -->
         <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-buttons/dist/global/ej2-buttons.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>
         <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
         <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-navigations/dist/global/ej2-navigations.min.js" type="text/javascript"></script>
   </head>
   <body>
       <!--target element-->
        <div id="target">Right click / Touch hold to open the ContextMenu</div>
        <!--element which is going to render-->
        <ul id="contextmenu"></ul>
        <script>
            ej.base.enableRipple(true);

            var menuItems = [
            {
              text: 'Cut'
            },
            {
              text: 'Copy'
            },
            {
              text: 'Paste'
            },
            {
              separator: true
            },
            {
              text: 'Font'
            },
            {
              text: 'Paragraph'
            }];
            var menuOptions = {
              target: '#target',
              items: menuItems
            };

            // Initialize ContextMenu control.
            var menuObj = new ej.navigations.ContextMenu(menuOptions, '#contextmenu');
        </script>
   </body>
  </html>

The separator property should not be given along with the other fields in the MenuItem.

See Also