Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable right click menu in html page using jquery [duplicate]

Tags:

html

jquery

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();
    });
});
like image 401
M.J.Ahmadi Avatar asked May 30 '14 06:05

M.J.Ahmadi


People also ask

How do I disable right click in HTML?

Disable right click menu in html page using jquery. JavaScript Code: $(document). bind("contextmenu",function(e){ return false; });

How do I restrict copy and paste in jQuery?

Projects In JavaScript & JQuery To disable cut, copy and paste of a content in jQuery, use the jQuery bind() function.

How do you disable right click on images jQuery?

$('img'). bind("contextmenu",function(e){ return false; }); That code will disable right-click on every image on your page. If you want to be more selective, assign the specific images you want to disable right-click for their own unique class, and then use that class name as the selector in place of the 'img' tag.


1 Answers

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();
     });
 });
like image 88
reyaner Avatar answered Oct 01 '22 19:10

reyaner