Answer the question
In order to leave comments, you need to log in
How to get selected text + n characters on sides?
I found this function to get the selected text:
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection(5).toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
Answer the question
In order to leave comments, you need to log in
Here is a simple example, no checks. For document.selection is done in a similar way.
function getSelectionText(d) {
var string = window.getSelection();
return string.baseNode.data.substring(string.anchorOffset - d, string.focusOffset + d);
}
var string = getSelectionText(10);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question