Skip to content

Commit 7a22327

Browse files
committed
Document how to create multiple menu instances on the same DOM element
Update mar10#91
1 parent 0530ecc commit 7a22327

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ Alternatively a handler may be bound, so this is equivalent:
406406
```js
407407
$("#container").bind("contextmenuselect", function(event, ui) {
408408
alert("select " + ui.cmd + " on " + ui.target.text());
409-
}
409+
});
410410
```
411411

412412
<dl>
@@ -525,6 +525,43 @@ $(document).contextmenu({
525525
```
526526
527527
528+
### [Howto] Bind different contextmenus to the same DOM element
529+
530+
This is especially useful if we want to bind contextmenus for different selectors
531+
to the `document` element, in order to make them global:
532+
533+
```js
534+
$(document).contextmenu({
535+
delegate: ".hasmenu",
536+
menu: ...,
537+
select: function(event, ui) {
538+
alert("select contextmenu 1" + ui.cmd + " on " + ui.target.text());
539+
}
540+
});
541+
```
542+
543+
Another call to `$(document).contextmenu({...})` would destroy the previous
544+
instance, because the [jQuery Widget Factory](https://learn.jquery.com/jquery-ui/widget-factory/)
545+
only allows one instance per element.
546+
547+
The soulution is to create new widget with another name but identical functionality:
548+
549+
```js
550+
// 1. Create and register another widget that inherits directly from
551+
// jquery-ui-contextmenu:
552+
$.widget("moogle.contextmenu2", $.moogle.contextmenu, {});
553+
// 2. Now we can bind this new widget to the same DOM element without
554+
// destroying a previous widget.
555+
$(document).contextmenu2({
556+
delegate: ".hasmenu2",
557+
menu: ...,
558+
select: function(event, ui) {
559+
alert("select contextmenu2" + ui.cmd + " on " + ui.target.text());
560+
}
561+
});
562+
```
563+
564+
528565
# Credits
529566
530567
Thanks to all [contributors](https://github.com/mar10/jquery-ui-contextmenu/contributors).

0 commit comments

Comments
 (0)