Categories
JavaScript Answers

How to disable dragging an image from an HTML page with JavaScript?

Spread the love

To disable dragging an image from an HTML page with JavaScript, we stop the default action for the dragstart event.

For instance, we write

document.getElementById("my-image").ondragstart = () => {
  return false;
};

to select the img element with getElementById.

Then we set its ondragstart property to a function that returns false to stop the default drag action to stop the img element from being dragged.

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 *