JavaScript – Disable the drag and drop on html element

JavaScript – Disable the drag and drop on html element

In this post we will give you information about JavaScript – Disable the drag and drop on html element. Hear we will give you detail about JavaScript – Disable the drag and drop on html elementAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to disable drag and drop on an html element using JavaScript.

Consider, that we have the following div element in our HTML:

<div id="container">  <h1>Placeholder image</h1>  <img src="https://via.placeholder.com/150" /></div>

To disable the drag and drop on an element, first we need to access that element inside the JavaScript using the document.getElementById() method, then add the dragstart and drop events to the element and call the event.preventDefault() method inside it

The event.preventDefault() method prevents the browser from default actions, so it disables the element from drag and drop.

Here is an example:

<div id="container">  <h1>Placehodler image</h1>  <img src="https://via.placeholder.com/150" /></div>
const divEl = document.getElementById("container");divEl.addEventListener("dragstart",(event)=>{  event.preventDefault();})div.addEventListener('drop', (event) => {  e.preventDefault();})

In the example above, we have added the dragstart and drop events to the div element and called the event.preventDefault() method inside it, so it prevents the user from dragging and dropping.

The HTML dragstart event is fired when a user tries to drag an element inside the webpage.

The HTML drop event is fired when a user tries to drop an element inside the webpage.

Similarly, we can also disable the drag and drop on an element by calling the event.preventDefault() method inside a mousedown event handler function.

Here is an example:

<div id="container">  <h1>Placehodler image</h1>  <img src="https://via.placeholder.com/150" /></div>
const divEl = document.getElementById("container");divEl.addEventListener("mousedown",(event)=>{  event.preventDefault();})

The mousedown event is fired at an element when a user presses the mouse button.

Disable Drag and Drop using CSS

We can also disable drag and drop on the html elements by setting the css pointer-events property to none.

When we set a pointer-events property to a value “none”, the element doesn’t respond to any cursor or touch events.

Here is an example:

<div id="container">  <h1>Placehodler image</h1>  <img src="https://via.placeholder.com/150" /></div>
#container{    pointer-events: none;}

Hope this code and post will helped you for implement JavaScript – Disable the drag and drop on html element. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US