How to get the text selection in Javascript

To get the text selection in Javascript you can use the following snippet.

Sample Javascript

function GetTextSelection() {
	return (!!document.getSelection) ? document.getSelection() :
	       (!!window.getSelection)   ? window.getSelection() :
	       document.selection.createRange().text;
}

2 thoughts on “How to get the text selection in Javascript

Leave a Reply