Categories
JavaScript Answers jQuery

How to Disable Right-Click on Images Using jQuery?

Spread the love

To disable right-click on images using jQuery, we can listen to the contextmenu event on the image element.

Then in the event handler callback, we can return false to prevent right-clicks on images.

For instance, if we have the following image:

<img src='https://picsum.photos/200/300'>

Then we can write:

$('img').bind('contextmenu', (e) => {  
  return false;  
});

to get all image elements with $(‘img’) .

Then we call bind with 'contextmenu' to listen to the contextmenu event.

And in the event handler callback, we return false .

Now when we right-click on the image, we shouldn’t see any context menu displayed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *