Search results

Change menu items dynamically in JavaScript Context Menu control

31 Mar 2023 / 2 minutes to read

The items visible in the ContextMenu can be changed dynamically based on the target in which you open the ContextMenu. To achieve this behavior, initialize ContextMenu with all items using items property and then based on the context you open hide/show required items using hideItems/showItems method in beforeOpen event.

In the following example, the datasource for Clipboard div is Cut, Copy, Paste and for the Editor div is Add, Edit, Delete is changed on beforeOpen event using hideItems and showItems method.

Copied to clipboard
import { ContextMenu, MenuItemModel, ContextMenuModel, BeforeOpenCloseMenuEventArgs } from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';


// Initialize menu items.
let menuItems: MenuItemModel[] = [
        text: 'Cut'
        text: 'Copy'
        text: 'Paste'
        text: 'Add'
        text: 'Edit'
        text: 'Delete'

// Initialize ContextMenu options.
let menuOptions: ContextMenuModel = {
        target: '#target .e-div',
        items: menuItems,
        beforeOpen: (args: BeforeOpenCloseMenuEventArgs) => {
          // To hide/show items on right click.
          if (( as HTMLElement).id === 'right') {
            menuObj.hideItems(['Cut', 'Copy', 'Paste']);
            menuObj.showItems(['Add', 'Edit', 'Delete']);
          } else if (( as HTMLElement).id === 'left') {
            menuObj.showItems(['Cut', 'Copy', 'Paste']);

// Initialize ContextMenu component.
let menuObj: ContextMenu = new ContextMenu(menuOptions, '#contextmenu');
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

    <title>Essential JS 2</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
    <meta name="description" content="Essential JS 2" />
    <meta name="author" content="Syncfusion" />
    <link href="//" rel="stylesheet" />
    <link href="//" rel="stylesheet" />
    <link href="//" rel="stylesheet" />
    <link href="//" rel="stylesheet" />
    <link href="//" rel="stylesheet" />
    <link href="//" rel="stylesheet" />

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

    <!--system js reference and configuration-->
    <script src=""></script>
    <script src="systemjs.config.js"></script>

    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div id="target">
            <div id='left' class='e-div'>Clipboard</div>
            <div id='right' class='e-div'>Editor</div>
        <!--element which is going to render-->
        <ul id="contextmenu"></ul>

Copied to clipboard
#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;

#container {
  visibility: hidden;

#target {
  user-select: none;

.e-div {
  width: 40%;
  border: 1px dashed;
  height: 150px;
  margin: 10px;
  text-align: center;
  line-height: 150px;
  display: inline-block;