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:
- How To Get The Last Character Of A String In JavaScript
- Remove The Last Character Of A String In JavaScript
- How To Validate An Email Address In JavaScript
- How To Check If An Input Field Is Empty In JavaScript
- Check If An Input Field Is A Number In JavaScript
- Confirm Password Validation In JavaScript
- How To Print A PDF File Using JavaScript
- Calculate The Number Of Days Between Two Dates In JavaScript
- How To Compare Two Dates In JavaScript
- Calculate Age With Birth Date YYYYMMDD In JavaScript
- How To Append or Add Text To A DIV Using JavaScript
- How To Get The Text Of HTML Element In JavaScript
- How To Change The Text Inside A DIV Element In JavaScript
- Show/Hide Multiple DIVs In JavaScript
- Show A DIV After X Seconds In JavaScript
- Display A JavaScript Variable In An HTML Page
- How To Generate A Random Number In JavaScript
- Bubble Sort In JavaScript
- Insertion Sort In JavaScript
- Selection Sort In JavaScript
- How To Remove A Specific Item From An Array In JavaScript
- Merge Sort In JavaScript
- Round To 2 Decimal Places In JavaScript
- SetInterval() and setTimeout() Methods In JavaScript
- Generate A Unique ID In JavaScript
- Caesar Cipher In JavaScript
- How To Reverse A String In JavaScript
- How To Loop Through A Plain JavaScript Object
- How To Open A URL In A New Tab Using JavaScript?