4

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?

Keith Palmer Jr.
  • 27,666
  • 16
  • 68
  • 105
  • 1
    In the onclick event, check if text is selected. If so, abort. ??? – ErikE Apr 25 '13 at 20:33
  • Why are you assuming that highlighting the text means they want to copy/paste? – Ian Apr 25 '13 at 20:35
  • 1
    I think you would want to check after `mouseup` if any text has been selected. – Unexpected Pair of Colons Apr 25 '13 at 20:35
  • wait, if the user is successful at highlighting the text then he/she would of copied the text already and not clicked. If the user was unable to highlight the text and accidentally clicked, then the text is not highlighted. I don't understand the use case – Huangism Apr 25 '13 at 20:35
  • @Ian it may not mean they want to copy text. But it's **pretty darn hard** to copy specific text *without* highlighting first. :) – ErikE Apr 25 '13 at 20:35
  • 1
    @ErikE Very true. I guess the OP has "e.g.", so they weren't **only** referring to copy/paste – Ian Apr 25 '13 at 20:37
  • 10
    @PalashMondal jQuery *is* JavaScript. The question is perfectly legitimate, the tag indicates that vanilla JS or jQuery is acceptable. – Madbreaks Apr 25 '13 at 20:38
  • @Huangism What do you mean, "if the user is successful at highlighting the text then... copied ... already"? The act of highlighting (mousedown->drag->mouseup[click]) is the *exact thing* that causes the `onclick` to occur, *before* any copy action can be performed. – ErikE Apr 25 '13 at 20:40
  • 1
    well it's not clear if OP wants a jquery solution since he specifically said javascript, anyway this is minor – Huangism Apr 25 '13 at 20:41
  • I am on a mac, I cannot highlight the text in an anchor link starting in the middle of the link text. I could however start with one end and highlight to the middle of the link and it does not trigger any click event – Huangism Apr 25 '13 at 20:42
  • @Huangism What link text? No one said anything about a hyperlink or `` element. It's just a `div`. – ErikE Apr 25 '13 at 20:43
  • well divs that's a different story – Huangism Apr 25 '13 at 20:44
  • @PalashMondal If some poor sucker gives a plain-vanilla javascript answer because he misses that the question is tagged `jQuery`, too, that won't exactly hurt anything, will it? – ErikE Apr 25 '13 at 20:44
  • 1
    It doesn't matter what the question is. If they're asking about Javascript, and tag jQuery as well, both are fair game. Since jQuery *is* Javascript, the question doesn't need to change and it's not even that important. jQuery is tagged, so it can be used in an answer validly – Ian Apr 25 '13 at 20:44
  • it's always possible that the jquery tag was a mistake, since lot of people knows javascript and does not know jquery, let's have the OP edit the question to end this discussion – Huangism Apr 25 '13 at 20:47
  • Please everyone remove your comments here, not related to the question.. – palaѕн Apr 25 '13 at 20:53
  • 1
    Edited - I don't care if it's plain old javascript or uses the JQuery lib. As long as it works, I'm happy. :-) – Keith Palmer Jr. Apr 25 '13 at 20:55
  • Think is is a duplicate. See http://stackoverflow.com/questions/4712310/javascript-how-to-detect-if-a-word-is-highlighted – cirrus Apr 25 '13 at 21:42

1 Answers1

2

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/

Community
  • 1
  • 1
4m1r
  • 12,234
  • 9
  • 46
  • 58