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

Spread the love

Sometimes, we want to disable dragging an image from an HTML page with JavaScript.

In this article, we’ll look at how to disable dragging an image from an HTML page with JavaScript.

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

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

For instance, we write

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

to select the element we want to disable dragging for.

Then we set its ondragstart property to a function that returns false to disable dragging on the element.

Conclusion

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

Leave a Reply

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