How to prevent dragging of ghost image with JavaScript?

Spread the love

Sometimes, we want to prevent dragging of ghost image with JavaScript.

In this article, we’ll look at how to prevent dragging of ghost image with JavaScript.

How to prevent dragging of ghost image with JavaScript?

To prevent dragging of ghost image with JavaScript, we use the setAttribute method.

For instance, we write

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

to add an img element.

then we write

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

to select the img element with getElementById.

And then we call setAttribute with 'draggable' and false to disable dragging.

Conclusion

To prevent dragging of ghost image with JavaScript, we use the setAttribute method.

Leave a Reply

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