How to select text without triggering a click using jquery?

I want users to be able to click on an element, as well as select the same element. If they click, then they should behave like a click, but if they go out to mousedown and select the text, it should not behave like a click (since this is not a click). jQuery launches the click if you are mousedown, and then after 1 million years, pull the mouse. I understand what you can do:

$myElm.on("mousedown.tmp", function () {
    $myElm.off("mouTsedown.tmp");
    $myElm.on("mouseup", function () {
        $myElm.off("mouseup.tmp")
    }
    }); // 

correct for spelling and factor in other event handlers. I mean roughly.

I do not want to use the plugin.

+4
source share
3 answers

Here is my fiddle solution, I hope this is what you are looking for.

mouseup() . click , .

 var selection = window.getSelection();
      if(selection == 0)
       // Click event code goes here

.

$(function(){
  $("div").click(function(){

      var selection = window.getSelection();
      if(selection == 0)
      {
          // click event code goes here.
      }
  });
  $("div").mouseup(function(){
    document.execCommand("BackColor", false, "yellow"); // highlight selected text
      document.designMode = "off";
  });    
  $("div").mousedown(function(){
     document.designMode = "on"; // Turn on design mode
  });
 });
+3
var mydate = new Date()
var downtime;

$('#div').on('mousedown', function() {
    downtime = mydate.getTime();
});

$('#div').on('mouseup', function() {
    if((mydate.getTime() - downtime) < 500) {
        //perform click function 
    }
    else {
        //highlight text
    }
});
+1

Keep track of the mouse position on mousedown. Compare this to the mouse position on the mouse. If the mouse has moved a certain number of pixels, prevent the default event.

event.preventDefault();
0
source

All Articles