Fork on GitHub

jQuery contextMenu

Contextmenu plugin & polyfill

The contextMenu Plugin was designed for web applications in need of menus on a possibly large amount of objects. Unlike implementations as a beautiful site's or trendskitchens' this contextMenu treats the menu as the primary object. That means, that a single menu is defined that can be used by multiple objects. Unlike the mentioned plugins, contextMenu doesn't need to bind itself to triggering objects. This allows injecting and removing triggers without having to re-initialize or update contextMenu.

context menu rendered by $.contextMenu

contextMenu can provide a simple list of clickable commands, or offer an in-menu form. This makes very simple attribute modification possible. See the input example.

Once a menu is registered, it cannot be altered. That means no commands can be added or removed from the menu. This allows contextMenu to keep a single definition in memory, which enables it to work with hundreds of trigger objects. contextMenu knows the two callbacks show and hide which can be used to update the state of commands within the menu. This allows en/disabling commands, changing icons or updating the values of contained <input> elements.

As of version 1.5 context menus can be created dynamically. That means the described behavior (once created, cannot be altered) still applies - but can be circumvented. Menus can be created on demand and they can be different depending on the triggering element.

Getting started

If you use NPM, install the jquery-contextmenu and include it in your build process.

If you just want to load the library into your website you can either donwload the contents of the dist folder or use cdnjs.com.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.contextMenu.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.contextMenu.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.ui.position.js"></script>
  1. Download contents of the dist folder of this library to you project site, for example to scripts/contextmenu
  2. <script src="scripts/jquery-3.2.1.min.js"></script>
  3. <script src="scripts/contextmenu/jquery.contextMenu.js"></script>
  4. <script src="scripts/contextmenu/jquery.ui.position.min.js"></script>
  5. <link rel="stylesheet" href="scripts/contextmenu/jquery.contextMenu.min.css">

Features

  • trigger contextMenu with right-click, left-click, hover or own custom trigger events
  • delegated event handling removing the need for re-initialization when trigger objects are added / removed
  • dynamic on-demand menu creation
  • optional icons for commands
  • input elements (text, textarea, checkbox, radio, select) within the menu
  • custom html elements (command free)
  • show/hide callbacks to update the state of commands
  • small memory footprint even with hundreds of trigger objects
  • adjust position of menu to fit in viewport
  • enable / disable commands
  • nested sub-menus
  • full keyboard interaction
  • HTML5 <menu> support
  • CSS is for styling, javascript is not...

Authors

License

$.contextMenu is published under the MIT license.