Cancel the onclick event if I selected the text

I have a div that when I clicked, wants to perform an action.

This div contains some text.

If the user selects the text (for example, they try to copy / paste, and not click on it), I want to cancel the onclick event so that the action does not happen if they just tried to select, and not actually click.

Is there a way to do this using jQuery or plain old javascript?

+4
source share
1 answer

Try to detect the onmouseup event:

document.onmouseup = doSomethingWithSelectedText; 

If the text is selected, a value will be used in window.getSelection to determine which event you should fire.

Check out this post / fiddle:

Javascript: how to detect word highlighting http://jsfiddle.net/timdown/SW54T/

+2
source

All Articles