Reputation: 276
I have an app which handles a lot of jQuery events, and I need a way to differentiate simple clicks from text selections.
I'd like to disable all click events when I detect a text selection. Is this possible? I've thought of doing it like this:
// User presses mouse button
$(window).on('mousedown', function(){
// User moves mouse while pressing mouse button => selection
this.one('mousemove', function(){
// Disable click listener temporarily
$.fn.click = null;
// User releases mouse button => end of selection
}).on('mouseup', function(){
// Stop mousemove event & restore click listener
this.off('mousemove');
$.fn.click = function(){};
});
});
The code obviously won't work but serves to illustrate my goal. Does anyone know of a good way to achieve this?
Upvotes: 1
Views: 698
Reputation: 764
Just use flag, like this:
var flag = 0;
// User presses mouse button
$(window).on('mousedown', function(){
if (flag == 0) {
// do some if click event enabled
}
// User moves mouse while pressing mouse button => selection
this.one('mousemove', function(){
// Disable click listener temporarily
// $.fn.click = null;
flag=1;
// User releases mouse button => end of selection
}).on('mouseup', function(){
// Stop mousemove event & restore click listener
// this.off('mousemove');
// $.fn.click = function(){};
flag = 0;
});
});
Upvotes: 0
Reputation: 210
Something as simple as setting a flag would work for your example.
var flag = true;
...
if(flag === true)
{
--Click listener enabled code here--
}
else
{
--Click listener disabled code here--
}
All you have to do is determine when to enable or disable the flag.
Upvotes: 1