1

document.oncontextmenu=function(){ return false; };

Right click is disabled on my website,I want to enable it for a modal which will show an Image to download.

ALOK
  • 21
  • 4
  • pls add some relevant code to start with or people will delete your question. – Deadpool Jun 07 '20 at 03:44
  • 1
    [https://stackoverflow.com/questions/3230622/disable-enable-right-click-on-a-particular-part-of-the-html-body](https://stackoverflow.com/questions/3230622/disable-enable-right-click-on-a-particular-part-of-the-html-body) Easy to find info on this topic with a little research. – MilkyTech Jun 07 '20 at 03:53
  • 1
    In general, it isn't great to disable right-click anyway. Consider not doing that, unless you have a specific reason to do so. – Brad Jun 07 '20 at 05:03

1 Answers1

2

Right click at the document level is usually disabled by something like

document.addEventListener('contextmenu', event => {
    event.preventDefault();
});

You can stop the propagation of the event at the element you want and never let the bubbling event reach the document level.

element.addEventListener('contextmenu', event => {
    event.stopPropagation();
});

Note that this only works if preventDefault() was set in the event bubble phase and not in the capture phase.

Jeril Sebastian
  • 793
  • 7
  • 10