Skip to content

JavaScript Context Menu

Introducing the jSuites context menu, a dynamic JavaScript plugin designed to enrich web applications by enabling custom right-click context menus on HTML elements. Perfectly adaptable across different web frameworks, this tool is optimized for a smooth and consistent user experience on all devices, including mobile. With its broad customization capabilities, you can easily integrate submenus, icons, and specific event handlers to create a more interactive and user-friendly interface. Key Features:

  • Framework Flexibility: Easily integrate with React, Angular, Vue.js and other frameworks.
  • Mobile Optimization: Ensures a responsive and intuitive experience on mobile devices.
  • Extensive Customization: Offers various options, including custom events, submenus, and icon inclusion, for a tailored application feel.
  • Versatile Implementation: This can be used as a standalone JavaScript plugin or as a web component, providing flexibility in application development.
PropertyDescription
items: Array of objectsArray of item object descriptions.
onclick: functionGlobal onclick event. function(instance, event)
PropertyDescription
type: stringContext menu item type: line | divisor | default
icon: stringContext menu icon key. (Material icon key icon identification)
id: stringHTML id property of the item DOM element
disabled: booleanThe item is disabled
onclick: function(element: HTMLElement, event: e) : voidSpecific onclick event for the element.
shortcut: stringA short description or instruction for the item. Normally a shortcut.
tooltip: stringShow this text when the user mouse over the element
submenu: Array of objectsSubmenu items

Using this example, incorporate a custom context menu into your web application. This implementation showcases setting up a context menu as a web component, allowing for seamless integration and enhanced interactivity within your application.

About CTRL+A
Go to Jspreadsheet Pro website

Save
Submenus ►
Sub menu 1Ctrl + X
right click inside the square to open the contextmenu
<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<script src="https://jsuites.net/v6/jsuites.webcomponents.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<jsuites-contextmenu id='contextmenu-webcomponent'>
    <div onclick="alert('About is clicked')"><a>About</a> <span>CTRL+A</span></div>
    <div onclick="window.open('https://jspreadsheet.com/v7')">Go to Jspreadsheet Pro website</div>
    <hr>
    <div data-icon="save">Save</div>
    <div>
        <a>Submenus</a>
        <span>►</span>
        <div class="jcontextmenu" tabindex="900">
            <div><a>Sub menu 1</a><span>Ctrl + X</span></div>
        </div>
    </div>
</jsuites-contextmenu>

<div aria-contextmenu-id="contextmenu-webcomponent" style="border:1px solid grey;width:400px;height:300px;"></div>
<i class="small">right click inside the square to open the contextmenu</i>
</html>

Easily add a context menu to your web projects using only vanilla JavaScript for a lightweight and customizable solution.

right click inside the square to open the contextmenu
<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<div id='container' style='border:1px solid grey;width:400px;height:300px;'></div>
<i class="small">right click inside the square to open the contextmenu</i>

<div id='contextmenu'></div>

<script>
var contextMenu = jSuites.contextmenu(document.getElementById('contextmenu'), {
    items:[
        {
            title:'Copy',
            shortcut:'Ctrl + C',
            onclick:function() {
                alert('Copy');
            },
            tooltip: 'Method to copy the text',
        },
        {
            type:'line'
        },
        {
            title:'Visit the website',
            onclick:function() {
                window.open('https://jspreadsheet.com/v7');
            }
        },
        {
            title:'Google',
            disabled: true,
            onclick:function() {
                alert('jSuites v2')
            }
        },
        {
            title:'About',
            onclick:function() {
                alert('jSuites v2')
            }
        },
    ],
    onclick:function() {
        contextMenu.close(false);
    }
});

document.getElementById('container').addEventListener("contextmenu", function(e) {
    contextMenu.open(e);
    e.preventDefault();
});
</script>