Applying a Right-Click

$("#menu").hide(); $("#main").on("mousedown", function(e){   if(e.button == 2){ $("#menu").show(); } }); $("#main").on("mouseup", function(e){   if(e.button == 2){ $("#menu").hide(); } }); $('#main').on("contextmenu", function(e){   return false; });

Applying a right-click to an element requires some additional actions because the default browser action is to bring up a right-click menu for the browser page.

The first thing you need to do in your event handler is to determine which mouse button was clicked by looking at the event.button attribute, which contains 0 for left, 1 for center, and 2 for right.

You also need to suppress the default browser contextmenu behavior, as shown in lines ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.