2

When the contextmenu is shown,

if the user clicks out of the contextmenu,

should hide it.

How to implement this functionality?

Is it possible to listen for click_out,and when it's detected,hide contextmenu and clear that listener?

3 Answers 3

2

You can bind to the body.click to hide it. Any click event on any other element bubbles up to the body eventually:

$('body').click(function() {
   $('#menu').hide();
});

Above example is assuming your custom menu has the ID of 'menu'. Replace as needed.

Depending on how your menu works (if you have nested menus you can click to open) you might want to bind something to the clicks inside it to stop the event using e.stopPropagation();

To clear the listener you can do:

$('body').click(function() {
   $('#menu').hide();
   $(this).unbind('click');
});
Sign up to request clarification or add additional context in comments.

Comments

1

You could add an onclick handler to the document and then check if the event target is the menu or not. If the click is on the menu, do nothing, if not hide it.

jQuery(document).click(function(event) {
  if(event.target==$('context-menu'){
    $('context-menu').hide();
    $(this).unbind('click');
  }
}):

1 Comment

I just updated the answer to add the unbind, you'll probably want to attach this click handler to the document whenever the context-menu is shown.
0

Here is a snippet of some working code:

document.onclick = Tree.hideContext;

    Tree = {
        hideContext: function() {
            $("#context").hide();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.