Keith Palmer Jr.
Keith Palmer Jr.

Reputation: 27952

Cancel an onclick event if I highlighted text

I have a div that, when clicked, I want to an action to happen.

That div contains some text.

If the user highlights the text (e.g. they are trying to copy/paste, rather than actually click it) then I want to cancel the onclick event so that the action doesn't happen if they were just trying to highlight, not actually click.

Is there a way to do this with JQuery or plain old Javascript?

Upvotes: 5

Views: 371

Answers (1)

4m1r
4m1r

Reputation: 12542

Try detecting the onmouseup event:

document.onmouseup = doSomethingWithSelectedText;

If the text is highlighted, you will have a value in window.getSelection use that to determine what kind of event you should be firing.

Check out this post / fiddle:

Javascript: How to detect if a word is highlighted http://jsfiddle.net/timdown/SW54T/

Upvotes: 3

Related Questions