3

Possible Duplicate:
Selected text event trigger in Javascript
jquery: select text event

Is there a handler/listener in javascript which determines when text is selected in a certain element? Raw Javascript is fine, although something jQuery would be preferred. Thanks!

Community
  • 1
  • 1
Kpower
  • 1,237
  • 3
  • 11
  • 19

2 Answers2

4

Rangy comes with all kinds of goodies related to manipulating text selection: http://code.google.com/p/rangy/

Using it with jQuery is incredibly simple. For example:

$( 'body' ).on( 'mouseup', function(){
  var sel = rangy.getSelection();
  if( sel.length > 0 ){
    console.log( sel );
  }
});

Good luck!

Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
0

There is no event for selection, but you can use document.getSelection to access text that may be currently selected. You can bind a handler to onmouseup or onkeyup, etc.

Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27