1

I would like to retrieve any text a user may have highlighted with the mouse. I would like to be able to do so from within any arbitrary element. Is this possible?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bibs
  • 1,226
  • 2
  • 11
  • 14

2 Answers2

1

Absolutely!

Check this out.

http://www.java2s.com/Code/JavaScript/HTML/CapturingaTextSelection.htm

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Here's a function that works in all major browsers:

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type == "Text") {
        text = document.selection.createRange().text;
    }
    return text;
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536