Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect click but not drag using jQuery?

I have code that select text when user click the p tag but I don't want to do that when user select the text inside. Is it possible to detect click but without drag?

I have simple code like this:

$('.conteiner').on('click', 'p.xxx', function() {
   $(this).selection();
});

But click is executed even if I drag between mousedown and mouseup. The selection is a plugin that select text using getSelection or Range.

like image 234
jcubic Avatar asked Feb 18 '14 10:02

jcubic


1 Answers

Similar to this: Can you detect "dragging" in jQuery?

You can use mousedown and mouseup to detect whether there was a drag.

 $(function() {
    var isDragging = false;
    $(".conteiner")
    .mousedown(function() {
        $(window).mousemove(function() {
            isDragging = true;
            $(window).unbind("mousemove");
        });
    })
    .mouseup(function() {
        var wasDragging = isDragging;
        isDragging = false;
        $(window).unbind("mousemove");
        if (!wasDragging) {
            $(this).selection();
        }
    });
  });

Here is the plunker demo: http://embed.plnkr.co/Y7mrOP/

like image 177
vanna Avatar answered Sep 29 '22 17:09

vanna