Categories
JavaScript Answers

How to use CSS or JavaScript to prevent dragging of ghost image?

Spread the love

To use CSS or JavaScript to prevent dragging of ghost image, we set the draggable attribute to false.

For instance, we write

<img id="myImage" src="https://picsum.photos/30/30" />

to add an img element.

Then we disable dragging on it with

document.getElementById("myImage").setAttribute("draggable", false);

We select the img element with getElementById.

And then we call setAttribute to set the draggable attribute to false to disable dragging.

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 *