jQuery Context Menu Plugin
Written by Cory S.N. LaViska on September 14th, 2008
A context menu plugin for jQuery that features easy implementation, keyboard shortcuts, CSS styling, and control methods.
Contents
Overview
jQuery Context Menu is a context menu plugin for jQuery. It was designed to make implementing context menu functionality easy and requires minimal effort to configure.
There is another context menu for jQuery written by Chris Domigan. These two projects are completely unrelated and no source code was shared from Chris’ project.
Why reinvent the wheel? The goal of this plugin is to streamline the way actions are binded to menu items and to use 100% CSS for styling. Keyboard shortcuts were added for navigating the menu once it’s open, and there are five methods to allow you to control and clean-up context menus on the fly.
Features
- Valid XHTML
- Fully customizeable via CSS
- Easy to configure and implement
- Degrades gracefully
- Keyboard shortcuts to maximize accessibility
- Ability to enable/disable menu items on the fly
- Ability to enable/disable entire menu on the fly
Compatibility
jQuery Context Menu works in all browsers supported by jQuery. It has been fully tested in:
- Internet Explorer 6 & 7
- Firefox 2 & 3
- Safari 3
- Chrome (beta)
- Opera 9.5*
*Opera 9.5 has an option to allow scripts to detect right-clicks, but it is disabled by default. Furthermore, Opera still doesn’t allow JavaScript to disable the browser’s default context menu which causes a usability conflict.
Demo
View a live demonstration of the context menu plugin.
Download
Current version: Version 1.0 (14 September 2008)
This plugin is provided to you as-is, at absolutely no cost. If you would like to support its development, feel free to contribute any amount you prefer via PayPal. As always, you are welcome to contribute code for bug fixes and feature enhancements as well. Either way, thanks for supporting our efforts!
Usage
Dependencies
jQuery Context Menu requires jQuery 1.2.6 or above (or jQuery 1.2 with the dimensions plugin).
Creating
First, create a list in your HTML that will be the markup for your context menu:
<li class="edit">
<a href="#edit">Edit</a>
</li>
<li class="cut separator">
<a href="#cut">Cut</a>
</li>
<li class="copy">
<a href="#copy">Copy</a>
</li>
<li class="paste">
<a href="#paste">Paste</a>
</li>
<li class="delete">
<a href="#delete">Delete</a>
</li>
<li class="quit separator">
<a href="#quit">Quit</a>
</li>
</ul>
Actions are specified in the href attribute, preceeded by a # symbol. When selected, this is what will be passed back to the action parameter in the callback. You can add class attributes to the list items to assist with styling, but they have no functional meaning. Thus, class names do not have to correspond with actions.
Once you have created the markup for the context menu, bind it to one or more elements using JavaScript:
$("#selector").contextMenu({
menu: 'myMenu'
},
function(action, el, pos) {
alert(
'Action: ' + action + 'nn' +
'Element ID: ' + $(el).attr('id') + 'nn' +
'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)nn' +
'X: ' + pos.docX + ' Y: ' + pos.docY+ ' (relative to document)'
);
});
});
Make sure you change #selector to the appropriate jQuery selector that matches the element(s) you want to bind the context menu to. The selector can be any valid jQuery selector.
Methods
There are five methods you can use to control and clean-up the context menu after it has been instantiated.
| Method | Description |
|---|---|
| disableContextMenu() | Disables the context menu on all matching elements |
| enableContextMenu() | Re-enables the context menu on all matching elements |
| disableContextMenuItems('#option1,#option2,...') | Disables the specified options on all matching elements. If null is passed, all elements will be disabled. |
| enableContextMenuItems('#option1,#option2,...') | Enables the specified options on all matching elements. If null is passed, all elements will be enabled. |
| destroyContextMenu() | Unbinds the context menu from all matching elements |
As an example, to disable the context menu on a div with an ID of myDiv the code would look like this:
Styling
The context menu relies 100% on CSS for styling. To give your users an aesthetically pleasing experience, you should either use the included stylesheet or create your own. Refer to jquery.contextMenu.css to make any changes in the styles.
Callback
The callback function fires if and only if a user makes a valid selection from the context menu. Three arguments are passed which you can use to control what happens when a user makes a selection:
| Paramter | Description |
|---|---|
| action | The action that corresponds to the menu item that was selected (I.E. the href attributes less the # symbol) |
| el | The element object that triggered the context menu |
| pos.x | Horizontal position of mouse cursor when menu was clicked (relative to the element) |
| pos.y | Vertical position of mouse cursor when menu was clicked (relative to the element) |
| pos.docX | Horizontal position of mouse cursor when menu was clicked (relative to the document) |
| pos.docY | Vertical position of mouse cursor when menu was clicked (relative to the document) |
See the example above for starter code.
Licensing & Terms of Use
jQuery Context Menu is licensed under a Creative Commons License and is copyrighted ©2008 by Cory S.N. LaViska.









Comments
#1 Hans on Sep 16th, 2008
#2 Cory S.N. LaViska on Sep 16th, 2008
#3 vladm on Sep 17th, 2008
#4 Cory S.N. LaViska on Sep 17th, 2008
#5 Alexander on Sep 22nd, 2008
#6 James on Sep 22nd, 2008
#7 Mark Smith on Sep 27th, 2008
#8 Piotr Findeisen on Sep 30th, 2008
#9 Cory S.N. LaViska on Sep 30th, 2008
#10 Piotr Findeisen on Sep 30th, 2008
#11 Piotr Findeisen on Oct 8th, 2008
#12 Paul on Oct 15th, 2008
#13 Lisbeth on Oct 16th, 2008
#14 Cory S.N. LaViska on Oct 16th, 2008
#15 Martin on Oct 17th, 2008
#16 Cory S.N. LaViska on Oct 17th, 2008
#17 oxman on Oct 19th, 2008
#18 Rob on Oct 20th, 2008
#19 Cory S.N. LaViska on Oct 20th, 2008
#20 odt on Oct 23rd, 2008
#21 Lukasz on Oct 28th, 2008
#22 Cory S.N. LaViska on Oct 28th, 2008
#23 Lukasz on Oct 29th, 2008
#24 Yaroslav on Oct 29th, 2008
#25 Heath on Oct 29th, 2008
#26 Frank on Nov 1st, 2008
#27 Frank on Nov 1st, 2008
#28 Cory S.N. LaViska on Nov 1st, 2008
#29 Greg on Nov 4th, 2008
#30 Matt on Nov 10th, 2008
#31 Jeff on Nov 11th, 2008
#32 andrewb on Nov 19th, 2008
#33 Cory S.N. LaViska on Nov 20th, 2008
#34 Leon Sorokin on Nov 21st, 2008
#35 adam j. sontag on Nov 24th, 2008
#36 uprocka on Nov 26th, 2008
#37 anonymous on Dec 5th, 2008
#38 Marco on Dec 11th, 2008
#39 Tyler on Dec 11th, 2008
#40 Sedat Kumcu on Dec 11th, 2008
#41 Kikuchyo on Dec 13th, 2008
#42 Neal on Dec 19th, 2008
#43 Robin on Dec 19th, 2008
#44 Neal Chapman on Dec 20th, 2008
#45 HYL on Dec 31st, 2008