0

I'm trying to disable double click selection on certain elements. I have tried

$('#rand').on('dblclick', function(e) {
   e.preventDefault();
});

and many other variations. I can achieve the effect by preventing default on mousedown but that also disables selection by dragging. How can I disable the double click event without disabling drag and select?

Here's the fiddle.. http://jsfiddle.net/rBRRb/

P.S: I have read many similar questions, but din't find a solution that works for my case.

questions
  • 2,337
  • 4
  • 24
  • 39
  • I cannot give you better than http://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click?rq=1 which I am sure you already found. [Working fiddle](http://jsfiddle.net/rBRRb/2/). Btw, in your fiddle jQuery is not included even though your code uses it. – kapa May 23 '14 at 23:01
  • @kapa- Thank you, but it clears selection after it's done which is ok but not a great experience. – questions May 23 '14 at 23:07
  • That's why I'm saying I cannot give you better than this ;). – kapa May 23 '14 at 23:08
  • Why is it that I can prevent this behavior by preventing default on mousedown and not any other way? – questions May 23 '14 at 23:09
  • @MattGreen- It doesn't disables text selection, it removes it after selection.. – questions May 23 '14 at 23:11
  • @questions Because you simply cannot do that. You can disable selection. You can enable selection. But you cannot disable selection by double-clicking ONLY. – kapa May 23 '14 at 23:15

1 Answers1

0

Best solution I found was on this post.

$("#rand").on('mousedown', function (event) {
    //event.preventDefault();
});
$("#ran").on('mouseup', function (event) {
    //event.preventDefault();
});
$('#rand').on('dblclick', function (e) {
    if (window.getSelection) window.getSelection().removeAllRanges();
    else if (document.selection) document.selection.empty();
});

FIDDLE

As that is a browser's native behavior you can not "disable" it, but you can modify it like the above that removes the selection after it is made.

Community
  • 1
  • 1
MattSizzle
  • 3,145
  • 1
  • 22
  • 42