0

Is it possible to react to a click only when the user is not highlighting the text like facebook does in its chat windows?

$("#wrapper").on("click", '.content', $.proxy(function(e) {
// if ($(e.currentTarget).isHighlighted()) return false;
}, this));
Chris
  • 4,255
  • 7
  • 42
  • 83

2 Answers2

2

Insider your click handler, you can test to see if the document has any selection:

$("#wrapper").on("click", function() {
    if(!document.getSelection().toString().length) {
        console.log("No selection made");
    }
});

Here is a fiddle of the above.

If you need it to work in IE8, you'll need to use document.selection instead.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
2

Yes, it's possible.

You can use Selection API, and take care of cross-browser compatibility.

$("#wrapper").on("click", '.content', $.proxy(function(e) {
  var selection = window.getSelection();
  if (selection && selection.containsNode(e.target)) {//selection contains event target
    return false;
  }
}, this));

To be more accurate, you can check whether focusNode or anchorNode is the event target.

jasonslyvia
  • 2,529
  • 1
  • 24
  • 33