Ad

JQuery Context Menu In HTML/js Code

- 1 answer

I have an application with some HTML/js code. I am using jquery to show a contextmenu (Version: 1.6.6) in the $document element when I rightclick in a div of this document. With 'autohide' it closes when mouse leaves the HTML document, but I would like to close the context menu in some other situations. This is my first problem, how to close the menu programatically.

My code:

$(document).contextMenu({
  selector: '#' + panelId,
  autoHide: true,
  items: ...
  callback: function(key, options) {
    if (key === ...
  } 

I have a function where I need to close the menu, but all my tests fail:

function()
{
    $(document).contextmenu(false);
    $(document).trigger('contextmenu:hide');
    $trigger = $('#' + panelId);
    $trigger.contextMenu(false);
}

'panelId' is a value I get when I rightclick in the div, using 'oncontextmenu' attribute with 'myFunction' value. So 'myFunction' is called before displaying the context menu, and I save there the id, to be used to enable/disable items when the menu is 'about to show'

My second problem: If the context menu is open after rightclicking in a div element (saving the divId in 'myFunction'), and i rightclick in another div element, 'myFunction' is NOT called, so I can not save the new divId, and the "new" context menu has the same options than the previous one. How can I get the rightclick when the context menu is open? I need the new divId.

Ad

Answer

I have updated my plugin version to https://github.com/swisnl/jQuery-contextMenu/blob/master/src/jquery.contextMenu.js and my 2nd problem was solved.

Then I found how to close the menu, maybe it could help to anyone:

$('.context-menu-list').trigger('contextmenu:hide');

Regards, Diego

Ad
source: stackoverflow.com
Ad