Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing right-click on selected class in Javascript

I have a javascript that prevents right click on an HTML page:

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

I have a <input> tag on that same page with the name "Link" that I want the right click to happen on.

How can I achieve that?

like image 753
Cody Coderson Avatar asked Dec 19 '17 08:12

Cody Coderson


1 Answers

You can check and test e.target of the event:

document.addEventListener("contextmenu", function(e){
    if (e.target.tagName.toLowerCase() === 'input' && e.target.name === 'Link') {
      return; //pass
    }
    e.preventDefault(); // prevent others
}, false);
like image 176
hsz Avatar answered Oct 02 '22 21:10

hsz