I have multiple DOM elements with context menus. When one element is a child of the other and I invoke the context menu of the inner child, I also see the context menu from the parent. This is implemented with the jquery-ui.contextmenu plugin.
Is there a way to configure the plugin to prevent the parent's menu from being shown or am I going to have to handle all the click events manually and filter them so I show only the menu I want?
Following is my code:
HTML:
<!-- Add a child which will have a context menu -->
<div class="outer-child" id="outer-child">
Outer Child
<!-- inner child with its own context menu -->
<div class="inner-child" id="inner-child">
Inner Child
</div>
</div>
</div>
CSS:
.outer-child {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
height: 200px;
border: 1px solid red;
background: green;
}
.inner-child {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
border: 1px solid blue;
background: yellow;
}
JavaScript:
// create context menu on outer child
$("#outer-child").contextmenu({
menu: [
{title: "This is the Outer Menu", cmd: "outer-menu"}
],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});
// create context menu on inner child
$('#inner-child').contextmenu({
menu: [
{title: "Inner Menu", cmd: "inner-menu"}
],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});
You can find a jsfiddle demo here. (Right-click the inner box and see both context menus)
A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.
Definition. A control to create a hover menu. A hover menu works like a pop up menu in a desktop application. The hover menu can be activated on mouse click (pull down menu), on mouse over (pop up menu, help context) or on right mouse button click (context menu).
Definition: A contextual menu is a type of menu that appears on demand and contains a small set of relevant actions related to a control, an area of the interface, a piece of data in the application, or a view of the application.
Definition and Usage The contextmenu attribute specifies a context menu for an element. The context menu appears when a user right-clicks on the element. The value of the contextmenu attribute is the id of the <menu> element to open.
You can fix this issue by calling event.stopPropagation()
method in the beforeOpen
event of child element.
// create context menu on outer child
$("#outer-child").contextmenu({
menu: [{
title: "This is the Outer Menu",
cmd: "outer-menu"
}],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
},
});
// create context menu on inner child
$('#inner-child').contextmenu({
beforeOpen: function(event, ui) {
event.stopPropagation();
},
menu: [{
title: "Inner Menu",
cmd: "inner-menu"
}],
select: function(event, ui) {
alert("select " + ui.cmd + " on " + ui.target.text());
}
});
.outer-child {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
height: 200px;
border: 1px solid red;
background: green;
}
.inner-child {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
border: 1px solid blue;
background: yellow;
}
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="http://wwwendt.de/tech/demo/jquery-contextmenu/jquery.ui-contextmenu.js"></script>
<!-- Create an area to contain our editable components -->
<div class="container" id="container">
<!-- Add a child which will have a context menu -->
<div class="outer-child" id="outer-child">Outer Child
<!-- inner child with its own context menu -->
<div class="inner-child" id="inner-child">Inner Child</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With