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?
Asked
Active
Viewed 87 times
1
-
Duplicate: http://stackoverflow.com/questions/5379120/jquery-get-the-highlighted-text – Tim Down Jun 30 '11 at 22:32
2 Answers
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