Open context menu on right click in jQuery..but only if you click a certain div!


<div id='page_wrapper' style='border: 1px solid black; padding: 20px;'>
Right click in this div
<div id='contextMenu'>
<ul style='list-style: none;'>
<li><a href='#' target='_blank'>Open in New Window</a></li>
<li><a href='#' onclick='window.print()'>Print this Page</a></li>

/* Context menu only when you click in #page_wrapper (not in it's children) */
$(document).bind("contextmenu", function(event){
if( == 'page_wrapper'){
$("#contextMenu").css({"top": event.pageY + "px", "left": event.pageX + "px"}).show();
$(document).bind('click', function(){

Shane Stebner

Author: Shane Stebner

Shane is a successful web-developer specializing in responsive design, and the open-source LAMP stack. Over the years, he's gained in-depth experience with Stripe, Twilio, PhantomJs,, PayPal, Facebook, Twitter, and many other plugins/APIs.

comments powered by Disqus