14

I am looking for a simple way to hide/disable the right click context menu for the whole html page but not in some editable html elements just like input[text] and textarea using jquery.

I know this jquery code, but the below code will disable context menu in all html elements even in editable objects...

$(document).ready(function(){
    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });
});
M.J.Ahmadi
  • 3,931
  • 4
  • 17
  • 24

5 Answers5

19

you could check for tags that you need like this:

$(document).ready(function(){
    $(document).on("contextmenu",function(e){
        if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA")
             e.preventDefault();
     });
 });
reyaner
  • 2,799
  • 1
  • 12
  • 17
3

Give your id or class name to specify for which area you want to disable, for example:

$("#your_id").bind("contextmenu", function(e) {
    e.preventDefault();
});
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Nijanthan
  • 137
  • 2
  • 12
2

You can use this to disable for particular tag(replace img)

$(document).ready(function() {
$("img").bind("contextmenu",function(){
   return false;
});});

Courtesy to Peeter How to prevent Right Click option using jquery

Community
  • 1
  • 1
Jitu
  • 349
  • 1
  • 3
  • 16
  • 4
    At least attribute your source. Edit your answer to include this link: http://stackoverflow.com/questions/5618109/how-to-prevent-right-click-option-using-jquery and say that the answer was provided by Peeter. – Tarun May 30 '14 at 06:33
  • I missed it...done it now – Jitu May 30 '14 at 06:58
1

For binding it with all element you need:

$(document).ready(function(){
  $(document).bind("contextmenu",function(e){
     e.preventDefault();//or return false;
 });
 });
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Try this code

   <input type="text" id="test1"/>
<input type="text" id="test2"/>

JS Code:

$( "#test1" ).on( "copy cut paste drop", function() {
            return false;
    });

Jsfiddle

Tushar Dave
  • 220
  • 1
  • 5
  • Have you read my question carefully ?! – M.J.Ahmadi May 30 '14 at 06:44
  • @MJ.Ahmadi Sorry to misread your question,However if by disabling the right click you want to prevent your users from copy,cut,paste or drop something into your textbox i think inspite of disabling the right click you should use the jquery events that calls on cut copy and other events as a user can also use shortcut keys to cut copy and paste into textbox. I have updated my answer for that also. – Tushar Dave May 30 '14 at 07:04