Last updated: Apr 4, 2024
Reading time·4 min
mousedown
eventdraggable
to false
To disable drag and drop on HTML elements:
div
.addEventListener()
method to add event listeners for dragstart
and drop
.event.preventDefault()
method in the event handler functions.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box"> <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.addEventListener('dragstart', event => { event.preventDefault(); }); box.addEventListener('drop', event => { event.preventDefault(); });
We used the document.getElementById()
method to select the wrapper div
by its ID.
The next step is to add an event listener for the dragstart and drop events.
The dragstart
event is triggered when the user starts dragging an element or a
text selection.
The drop
event is triggered when an element or text selection is dropped on a
valid drop target.
Inside our event handler functions, we simply call the event.preventDefault()
method.
const box = document.getElementById('box'); box.addEventListener('dragstart', event => { event.preventDefault(); }); box.addEventListener('drop', event => { event.preventDefault(); });
The event.preventDefault() method tells the browser to not take the default action of the event.
This disables drag-and-drop events for all elements that are placed within the
div
that has an id of box
.
If you want to disable drag and drop for all elements in the document, you have
to add the event listeners on the document.body
element.
document.body.addEventListener('dragstart', event => { event.preventDefault(); }); document.body.addEventListener('drop', event => { event.preventDefault(); });
You can also disable drag-and-drop events for HTML elements using inline event handlers.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box" ondragstart="event.preventDefault()" ondrop="event.preventDefault()" > <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div> </body> </html>
We set the ondragstart
and ondrop
attributes on the div
element.
<div id="box" ondragstart="event.preventDefault()" ondrop="event.preventDefault()" > <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div>
When the events are triggered, we simply call the event.preventDefault()
method.
You could achieve the same result by returning false
straight away.
<div id="box" ondragstart="return false;" ondrop="return false;" > <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div>
If you want to disable drag-and-drop events for all elements in the document,
set the ondragstart
and ondrop
attributes on the body
element.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body ondragstart="event.preventDefault()" ondrop="event.preventDefault()" > <h2>bobbyhadz.com</h2> <div id="box"> <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div> </body> </html>
You could also achieve the same result by returning false
from the inline
event handlers.
<body ondragstart="return false;" ondrop="return false;" > <h2>bobbyhadz.com</h2> <div id="box"> <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div> </body>
mousedown
eventYou can also use the mousedown event to disable drag and drop on HTML elements.
The event is triggered at an element when the user's pointing device button is pressed while the pointer is inside the element.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box"> <img alt="favicon" src="https://www.google.com/favicon.ico" /> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); box.addEventListener('mousedown', event => { event.preventDefault(); });
The example disables drag and drop on all elements that are within the div
that has an id
of box
.
If you want to disable drag-and-drop events on all elements in the document, set
the event listener on the document.body
element.
const box = document.getElementById('box'); document.body.addEventListener('mousedown', event => { event.preventDefault(); });
draggable
to false
You can also disable drag and drop on an element by setting its
draggable
attribute to false
.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box"> <img alt="favicon" src="https://www.google.com/favicon.ico" draggable="false" /> </div> <script src="index.js"></script> </body> </html>
Notice that we set the draggable
attribute to false
on the img
element.
<img alt="favicon" src="https://www.google.com/favicon.ico" draggable="false" />
The draggable
attribute has to be set to false
directly on the element for
which you want to disable drag and drop.
The draggable
attribute determines whether the element can be dragged.
The attribute can have the following 2 values:
true
- the element can be dragged.false
- the element cannot be dragged.You can also set the draggable
attribute using JavaScript.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box"> <img id="favicon" alt="favicon" src="https://www.google.com/favicon.ico" /> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const favicon = document.getElementById('favicon'); favicon.setAttribute('draggable', false);
We used the setAttribute() method to set
the draggable
attribute to false
.
If you need to remove the draggable
attribute, use the removeAttribute()
method.
favicon.removeAttribute('draggable');
You can learn more about the related topics by checking out the following tutorials: