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();
    });
});
0

5 Answers 5

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();
     });
 });
Sign up to request clarification or add additional context in comments.

Comments

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();
});

Comments

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

2 Comments

At least attribute your source. Edit your answer to include this link: stackoverflow.com/questions/5618109/… and say that the answer was provided by Peeter.
I missed it...done it now
1

For binding it with all element you need:

$(document).ready(function(){
  $(document).bind("contextmenu",function(e){
     e.preventDefault();//or return false;
 });
 });

Comments

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

2 Comments

Have you read my question carefully ?!
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.