My application has many drag and drop features. While dragging I want the cursor to change to some grab cursor. Internet Explorer and Firefox work fine for this, but Chrome always changes the cursor to the text cursor.

- 4,555
- 31
- 31
- 45

- 765
- 1
- 6
- 11
-
2The question is adequately descriptive. Chrome overrides the cursor to the text selection cursor while dragging, even if a custom cursor is set on the element being dragged. A JSFiddle would be nice, but code is not necessary to explain the problem. – devios1 Feb 19 '13 at 20:02
8 Answers
None of these solutions worked for me because it's too much code.
I use a custom jQuery implementation to do the drag, and adding this line in the mousedown
handler did the trick for me in Chrome.
e.originalEvent.preventDefault();

- 3,823
- 5
- 34
- 39
-
1+10 I love the simplicity of this solution, and how well it works. Much better than attaching and detaching from mouse or selection events. – Chris Nolet Nov 30 '12 at 22:56
-
2@ChrisNolet well, to be fair, this is exactly the same solution as mine. I simply added an example of how you can attach the event handler. Tim is doing it with jQuery, which is not documented in his code, hence it looks shorter. You still have to attach the `mousedown` event somewhere to make it work. – Lorenzo Polidori Feb 11 '13 at 09:23
-
@Lorenzo. Looking at SO web programming topics I have the impression that 80% of people are using jQuery. – tim Jun 27 '13 at 19:26
-
3When I add the preventDefault() to the mousedown handler, the element can't be dragged anymore. Any solution to this? – lyxio Sep 20 '13 at 14:05
-
1@lyxio if you're not using jquery this will not work. Jquery has its own events but this solution attaches the prevent default to the original JavaScript event, leaving the jquery onmousedown alone so the dragging still fires – tim Nov 09 '13 at 21:20
Try turning off text selection event.
document.onselectstart = function(){ return false; }
This will disable any text selection on the page and it seems that browser starts to show custom cursors.
But remember that text selection will not work at all, so it's the best to do it only while dragging, and turn it on again just after that. Just attach function that doesn't return false:
document.onselectstart = function(){ return true; }

- 279
- 1
- 3
-
1Hey man thanx for you reply but I tried this also. but it didnt work for me. :( – user307635 Jun 04 '10 at 10:35
-
5I don't see why this answer got upvoted so many times. This is not the correct approach. What you need is to finely select the element that you want to apply this behavior to, and then prevent the default action for the `mousedown` event. See here: http://stackoverflow.com/questions/2745028/chrome-sets-cursor-to-text-while-dragging-why/12957678#12957678 – Lorenzo Polidori Oct 18 '12 at 15:03
-
1Nowhere near as simple as @tim's solution - and even Lorenzo's solution is a little complex. Just put `e.originalEvent.preventDefault();` in mouseDown as tim suggests. – Chris Nolet Nov 30 '12 at 22:58
-
-
2-1 because this is unnecessary, affects too much out of scope, and doesn't work. – devios1 Feb 19 '13 at 20:11
-
@LorenzoPolidori's comment should be the top answer here, as it worked (in my case in Safari 9.1.1 on OS X 10.11.5), where many other suggestions did not. – bluebinary Jul 07 '16 at 20:05
-
@LorenzoPolidori preventDefault() o mousedown did not work for me in Safari. seems like i had to do the onselectstart-approach. not sure, why it's not working in my case... – moritz.vieli Dec 27 '22 at 08:22
If you want to prevent the browser from selecting text within an element and showing the select cursor, you can do the following:
element.addEventListener("mousedown", function(e) { e.preventDefault(); }, false);

- 10,332
- 10
- 51
- 60
-
+1 not many ppl recognize that your answer is identical to mine except yours is plugin-agnostic. Since tons of people use jquery when coding web apps, I usually assume jquery even if the question is generic. – tim Sep 07 '14 at 00:39
-
@JosephLennox If anybody still needs to support IE8 for some reason: http://www.w3schools.com/browsers/browsers_explorer.asp – Lorenzo Polidori Aug 15 '16 at 16:43
-
@LorenzoPolidori I definitely agree general websites should not target IE8 support. Sadly, inside some offices, who might also be signing your paycheck, the IE8 usage rate is 100%. – Joseph Lennox Aug 15 '16 at 19:20
-
Pitfall
You cannot put the
document.onselectstart = function(){ return false; };
into your "mousedown" handler because onselectstart has already been triggered.
Solution
Thus, to have it working, you need to do it before the mousedown event. I did it in the mouseover event, since as soon as the mouse enters my element, I want it to be draggable, not selectable. Then you can put the
document.onselectstart = null;
call into the mouseout event. However, there's a catch. While dragging, the mouseover/mouseout event might be called. To counter that, in your dragging/mousedown event, set a flag_dragging to true and set it to false when dragging stops (mouseup). The mouseout function can check that flag before setting
document.onselectstart = null;
Example
I know you are not using any library, but here's a jQuery code sample that might help others.
var flag_dragging = false;//counter Chrome dragging/text selection issue
$(".dragme").mouseover(function(){
document.onselectstart = function(){ return false; };
}).mouseout(function(){
if(!flag_dragging){
document.onselectstart = null;
}
});
//make them draggable
$(".dragme").draggable({
start: function(event, ui){
flag_dragging = true;
}, stop: function(event, ui){
flag_dragging = false;
}
});

- 2,938
- 1
- 24
- 34
-
1Thanks for this. For my purposes `mouseenter` and `mouseleave` were what I wanted, as `mouseover` was triggered constantly while dragging within my element. – jberryman Jan 16 '12 at 18:18
I solved a same issue by making the Elements not selectable, and adding an active pseudo class on the draged elements:
* {
-webkit-user-select: none;
}
.your-class:active {
cursor: crosshair;
}

- 326
- 5
- 13
-
This works well, and you can add something like `input { -webkit-user-select: auto; }` to let users select text in input boxes. – Laurence Sep 27 '12 at 11:39
-
to have a complete answer: you need to add e.preventDefault(), since dragging's default action in WebKit is selection. – shex Nov 15 '12 at 13:29
-
This Solved the problem for me where e.originalEvent.preventDefault(); didnt. – Ed Fryed Aug 15 '13 at 05:59
I was facing almost same problem. I want cursor inside my DIV element and all its child to be the default, the CSS tips here helped in IE, FF and Opera, but not for Google Chrome. Here is what I have done in parent DIV:
<div ... onselectstart="return false;" ... > ... </div>
Now it is working fine. Hope this help.

- 41
- 1
Just use this line inside your mousedown
event
arguments[0].preventDefault();
You can also disable text selection by CSS adding this class to your draggable element
.nonselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

- 20,746
- 15
- 92
- 109
-
Thanks this works great! The `arguments[0].preventDefault();` was already enough. – A1rPun Dec 17 '13 at 20:43
I have a similar issue using jQuery UI draggable and sortable (ver. 1.8.1), and it's quite specific, so I assume that you are using same library.
Problem is caused by a bug in jQuery UI (actually a fix for other Safari bug). I just raised the issue in jQuery UI http://dev.jqueryui.com/ticket/5678 so I guess you will need to wait till it's fixed.
I've found a workaround for this, but it's quite hard-core, so you only use it if you really know what is going on ;)
if ($.browser.safari) {
$.ui.mouse.prototype.__mouseDown = $.ui.mouse.prototype._mouseDown;
$.ui.mouse.prototype._mouseDown = function(event){
event.preventDefault();
return $.ui.mouse.prototype.__mouseDown.apply(this, arguments);
}
}
It simply switches off the fix that's in jQuery UI code, so basically it may break something.

- 21
- 2
-
Hi, Thanx for your reply but I m not using any javascript library. So it cant help me :( – user307635 Jun 02 '10 at 12:16