Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Drag and Drop on HTML elements?

Tags:

javascript

css

I'm working on a web application for which I'm attempting to implement a full featured windowing system. Right now it's going very well, I'm only running into one minor issue. Sometimes when I go to drag a part of my application (most often the corner div of my window, which is supposed to trigger a resize operation) the web browser gets clever and thinks I mean to drag and drop something. End result, my action gets put on hold while the browser does its drag and drop thing.

Is there an easy way to disable the browser's drag and drop? I'd ideally like to be able to turn it off while the user is clicking on certain elements, but re-enable it so that users can still use their browser's normal functionality on the contents of my windows. I'm using jQuery, and although I wasn't able to find it browsing the docs, if you know a pure jQuery solution it would be excellent.

In short: I need to disable browser text selection and drag-and-drop functions while my user has the mouse button down, and restore that functionality when the user releases the mouse.

like image 874
Nicholas Flynt Avatar asked Apr 01 '09 08:04

Nicholas Flynt


People also ask

How do I turn off drag-and-drop in HTML?

We can disable drag and drop on HTML elements by setting the draggable attribute to false . We set draggable to false so we can't drag it. We add event listeners for the dragstart and drop events with addEventListener .

How do I stop images dragging in HTML?

One way to disable dragging an image from an HTML page is to set the ondragstart property of an image element to a function that returns false . We can write: const img = document.

How do you make something draggable in HTML?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.


2 Answers

This works. Try it.

<BODY ondragstart="return false;" ondrop="return false;"> 
like image 193
SyntaxError Avatar answered Sep 20 '22 01:09

SyntaxError


Try preventing default on mousedown event:

<div onmousedown="event.preventDefault ? event.preventDefault() : event.returnValue = false">asd</div> 

or

<div onmousedown="return false">asd</div> 
like image 27
Sergey Ilinsky Avatar answered Sep 18 '22 01:09

Sergey Ilinsky