jQuery .click() is triggering when selecting/highlighting text jQuery .click() is triggering when selecting/highlighting text javascript javascript

jQuery .click() is triggering when selecting/highlighting text


That's because a click is a mousedown followed by a mouseup. My suggestion is to check getSelection inside the click handler. If it's set, then you selected something, else you just clicked.

$('#click').click(function() {    var sel = getSelection().toString();    if(!sel){        alert("clicked");    }});​

DEMO: http://jsfiddle.net/ym5JX/3/


As I posted on comment, mosuedown + mouseup = click which is exactly what highlighting does. There is workaround for this.. see below,

var isClick = 0;$('#click').click(function() {    if (isClick == 1) {        alert("clicked");    }}).mousedown(function () {    isClick = 1;}).mousemove(function () {    isClick = 0;});

DEMO


jsFiddle: http://jsfiddle.net/ym5JX/8/

$('#click').click( function(){    if ( getSelection() == "" )    {        alert("clicked");    }});