Disable right click on form field

I need a general function that can disable right-clicking on form fields.

0
source share
4 answers
document.oncontextmenu = function(e) { var el = window.event.srcElement || e.target; var tp = el.tagName || ''; if ( tp.toLowerCase() == 'input' || tp.toLowerCase() == 'select' || tp.toLowerCase() == 'textarea' ){ return false; } }; 
+2
source

Impossible to do - not reliable and cross-browser (FF, IE, Chrome, Opera) in any case.

Browser hacks work in IE and FF, but there is a deeper problem: what are you trying to achieve? Limiting this user experience does not bring you anything (they already have their own valuable code, otherwise they will not see the page) and annoys users.

+1
source

This is probably a bad idea (annoying users because it contradicts the accepted standard user behavior), but you can do it in jQuery like this:

 $(function() { $(this).bind("contextmenu", function(e) { e.preventDefault(); }); }); 
0
source

If you need a special context menu for form fields, you can use this jQuery plugin:

http://www.javascripttoolbox.com/lib/contextmenu/index.php

Used as follows:

 var menu1 = [ { 'Option 1': function(menuItem,menu) { alert("You clicked Option 1!"); } }, { 'Option 2': function(menuItem,menu) { alert("You clicked Option 2!"); } } ]; $(function() { $('#myform:input').contextMenu(menu1,{theme:'vista'}); }); 

So, when the user clicks on any input fields of your form with the identifier 'myform', a personal context menu is displayed.

0
source

Source: https://habr.com/ru/post/1274180/