Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - disable select when dragging

I'm trying to do my own drag and drop function using jquery library. But everytime I mousedown on an image, then mousemove, my browser "highlights" or "selects" the image, which disrupts my mousemove operation.

How do I disable the select/highlight? I tried $('img').onselectstart = function() {return false;}, but that didn't work.

like image 395
John Avatar asked Feb 11 '11 23:02

John


2 Answers

You could prevent the default behaviour of the dragstart event...

$('img').bind('dragstart', function(event) { event.preventDefault(); });

jsFiddle.

like image 161
alex Avatar answered Oct 30 '22 04:10

alex


jQuery UI has an undocumented method that it uses to disable browser text selection. You can call it using this syntax:

$('IMG').disableSelection();

Remember that you need to be using jQuery UI (which I assume you are).

like image 35
claviska Avatar answered Oct 30 '22 03:10

claviska