Disable Right Click Using jQuery

There are situations when developers don’t want their users to use mouse’s right-click. It is not considered a best practice and user-friendly feature. It is kind of easy to use jQuery to block right-click event on a page.

Here is the simple snippet to do so:

$(document).ready(function(){
  //disable the whole document
  $(document).on("contextmenu",function(e){
    return false;
  });
  //disable on a specific element only
  $("#target").on("contextmenu",function(e){
    return false;
  });
  //or use .contextmenu();
  $("#target").contextmenu(function() {
    alert( "contextmenu() called" );
    return false;
  });
});

The contextmenu event is sent to an element when the mouse’s right-button is clicked on it, but before the context menu is displayed.

Leave a Comment

Your email address will not be published. Required fields are marked *

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close