Disable Drag and Drop on HTML elements?

JavascriptCss

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

This works. Try it.

<BODY ondragstart="return false;" ondrop="return false;">

Solution 2 - Javascript

Try preventing default on mousedown event:

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

or

<div onmousedown="return false">asd</div>

Solution 3 - Javascript

Solution 4 - Javascript

This might work: You can disable selecting with css3 for text, image and basically everything.

.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in IE 10.
     See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}

Of course only for the newer browsers. For more details check:

https://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting

Note: there are obvious a few UX downsides to this since it prevents users from easily copying text, be aware of this.

Solution 5 - Javascript

With jQuery it will be something like that:

$(document).ready(function() {
  $('#yourDiv').on('mousedown', function(e) {
      e.preventDefault();
  });
});

In my case I wanted to disable the user from drop text in the inputs so I used "drop" instead "mousedown".

$(document).ready(function() {
  $('input').on('drop', function(event) {
    event.preventDefault();
  });
});

Instead event.preventDefault() you can return false. Here's the difference.

And the code:

$(document).ready(function() {
  $('input').on('drop', function() {
    return false;
  });
});

Solution 6 - Javascript

This is a fiddle I always use with my Web applications:

$('body').on('dragstart drop', function(e){
    e.preventDefault();
    return false;
});

It will prevent anything on your app being dragged and dropped. Depending on tour needs, you can replace body selector with any container that childrens should not be dragged.

Solution 7 - Javascript

try this

$('#id').on('mousedown', function(event){
    event.preventDefault();
}

Solution 8 - Javascript

For input elements, this answer works for me.

I implemented it on a custom input component in Angular 4, but I think it could be implemented with pure JS.

HTML

<input type="text" [(ngModel)]="value" (ondragenter)="disableEvent($event)" 
(dragover)="disableEvent($event)" (ondrop)="disableEvent($event)"/>

Component definition (JS):

export class CustomInputComponent { 

  //component construction and attribute definitions

  disableEvent(event) {
	event.preventDefault();
	return false;
  }
}

Solution 9 - Javascript

using @SyntaxError's answer, https://stackoverflow.com/a/13745199/5134043

I've managed to do this in React; the only way I could figure out was to attach the ondragstart and ondrop methods to a ref, like so:

  const panelManagerBody = React.createRef<HTMLDivElement>();
  useEffect(() => {
    if (panelManagerBody.current) {
      panelManagerBody.current.ondragstart = () => false;
      panelManagerBody.current.ondrop = () => false;
    }
  }, [panelManagerBody]);

  return (
    <div ref={panelManagerBody}>

Solution 10 - Javascript

I will just leave it here. Helped me after I tried everything.

   $(document.body).bind("dragover", function(e) {
        e.preventDefault();
        return false;
   });

   $(document.body).bind("drop", function(e){
        e.preventDefault();
        return false;
   });

Solution 11 - Javascript

Question is old, but it's never too late to answer.

$(document).ready(function() {
  //prevent drag and drop
  const yourInput = document.getElementById('inputid');
  yourInput.ondrop = e => e.preventDefault();

  //prevent paste
  const Input = document.getElementById('inputid');
  Input.onpaste = e => e.preventDefault();
});

Solution 12 - Javascript

You can simply use draggable="false" on the element itself, else you can put

on-mousedown="preventDefaultDrag"
...
preventDefaultDrag: function(ev) {
   ev.preventDefault();
},

Solution 13 - Javascript

This JQuery Worked for me :-

$(document).ready(function() {
  $('#con_image').on('mousedown', function(e) {
      e.preventDefault();
  });
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankView Question on Stackoverflow
Solution 1 - JavascriptSyntaxErrorView Answer on Stackoverflow
Solution 2 - JavascriptSergey IlinskyView Answer on Stackoverflow
Solution 3 - Javascripticl7126View Answer on Stackoverflow
Solution 4 - JavascriptToshView Answer on Stackoverflow
Solution 5 - JavascriptVictor RomanoView Answer on Stackoverflow
Solution 6 - JavascriptLisView Answer on Stackoverflow
Solution 7 - JavascriptcrazyjuneView Answer on Stackoverflow
Solution 8 - JavascriptTito LeivaView Answer on Stackoverflow
Solution 9 - JavascriptkriskateView Answer on Stackoverflow
Solution 10 - JavascriptchecknovView Answer on Stackoverflow
Solution 11 - JavascriptDexterView Answer on Stackoverflow
Solution 12 - JavascriptTanimaView Answer on Stackoverflow
Solution 13 - JavascriptPiyushView Answer on Stackoverflow