Permission to right click on a selected class in Javascript

I have javascript that doesn't allow right-clicking on an HTML page:

document.addEventListener("contextmenu", function(e){ e.preventDefault(); }, false); 

I have an <input> on the same page with the name "Link" that I want the right click to take place.

How can i achieve this?

+5
source share
3 answers

Put the if inside your event listener:

 document.addEventListener("contextmenu", function(e){ if (e.target.name !== "Link") { e.preventDefault(); } }, false); 

So basically it says: when the target element does not have a Link name, right-click.

+1
source

You can check and test e.target events:

 document.addEventListener("contextmenu", function(e){ if (e.target.tagName.toLowerCase() === 'input' && e.target.name === 'Link') { return; //pass } e.preventDefault(); // prevent others }, false); 
+5
source

 <div> This is the Phone and NO ONE SHOULD RIGHT CLICK THIS! >:) </br> <img oncontextmenu="return false;" class="tlClogo" src="http://i.imgur.com/0atiS5C.jpg" style="height: 120px; width:120px;"> </div> </br></br></br></br> And this is the Keyboard, ofcourse yo can right click this :)</br> <img src="http://i.imgur.com/xkrKz1X.jpg" style="height: 120px; width:120px;"> 

Working script example

+1
source

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