O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required