You can disable the right-click context menu in JavaScript by using the oncontextmenu event. This event is triggered when the user right-clicks on an element. You can add an event listener to the document object to listen for this event and prevent the default behavior, which is to display the context menu. Here is an example of how to disable the right-click context menu:

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

This will prevent the context menu from appearing when the user right-clicks on the document.

If you want to disable right click on specific elements you can use the same function and attach the event listener to the specific element.

let element = document.querySelector("#your-element-id")
element.addEventListener('contextmenu', event => event.preventDefault());

It’s important to note that disabling the right-click context menu can make it difficult for users to access important features such as the “Save As” option, so it should be used with caution.

Also Read:

Categorized in: