English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Window getSelection() Method

JavaScript Window Object

getSelection()The method returns a Selection object that represents the range of text selected by the user or the current position of the insertion point.

Syntax:

window.getSelection()
var selObj = window.getSelection();
Test and See‹/›

Browser Compatibility

The numbers in the table specify the first browser version that fully supports the getSelection() method:

Method
getSelection()YesYesYesYes9

Technical Details

Return Value:Selection Object

More Examples

You can use CSS to set styles for selected text:

/* Firefox syntax */
::-moz-selection {
background: cyan; 
{}
 
::selection {
background: cyan;
{}
Test and See‹/›

Related References

CSS Referenceselection pseudo-element

JavaScript Window Object