JavaScript Document getSelection() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.getSelection() returns the document’s Selection object — the text the user highlighted, or the caret position when nothing is highlighted (MDN). It takes no parameters.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.getSelection. Start with this line.

JavaScript
document.getSelection()
CallWhat it does
document.getSelection()Primary signature.
document.getSelection(...)Call with the arguments your case needs.

Built into the browser. Call getSelection(...) on document — no import required.

Library declarations
JavaScript
document.getSelection()

Parameters

ParameterDescription
String formOptional. selection.toString() for the highlighted text

Return Value

Return ValueDescription
*Selection, or null without a browsing context

JavaScript Examples

Example 1 — Read the Selection Object

Call getSelection() and inspect type, range count, and collapsed state.

JavaScript
const selection = document.getSelection();

console.log(selection.type);
console.log(selection.rangeCount);
console.log(selection.isCollapsed);
Try It Yourself

How It Works

  1. Get the Selection. document.getSelection() returns the document selection (MDN).
  2. Check type. Often "Range" when text is highlighted, or "Caret" / "None" otherwise.
  3. Count ranges. rangeCount is usually 1 for a normal highlight.
  4. Collapsed? isCollapsed is true for a caret with no highlighted span.

Example 2 — Selected Text with toString()

MDN: call toString() explicitly when you need the highlighted string.

JavaScript
const selection = document.getSelection();
const selectedText = selection.toString();

console.log(selectedText);
console.log(typeof selectedText); // "string"
Try It Yourself

How It Works

  1. Get the Selection. Still an object, not a string.
  2. Call toString(). MDN: use this when you need the text value.
  3. Do not assume auto-string. alert(selection) may coerce; many APIs will not.
  4. Empty highlight. Collapsed selection → empty string "".

Example 3 — First Range with getRangeAt(0)

MDN sample path: read the first Range from the selection.

JavaScript
const selection = document.getSelection();

if (selection.rangeCount > 0) {
  const selRange = selection.getRangeAt(0);
  console.log(selRange.toString());
  console.log(selRange.collapsed);
}
Try It Yourself

How It Works

  1. Guard rangeCount. Avoid calling getRangeAt when there is no range.
  2. Read index 0. MDN example uses the first range for common single selections.
  3. Use the Range. toString() / collapsed describe that span.
  4. Build ranges too. Pair with createRange() when you create selections yourself.

Browser Support

Document.getSelection() is Baseline Widely available on MDN (since November 2017). Logos use the shared browser-image-sprite.png sprite from this project.

✓ Baseline Widely available

Document.getSelection()

Selection object for selected text and caret across all major browsers.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (legacy)
Yes
getSelection() Wide

Bottom line: Safe for reading page selections. Remember input/textarea quirks in some browsers, and that selection is not the same as focus. See the MDN Document.getSelection() reference.

Frequently Asked Questions

MDN: Document.getSelection() returns the Selection object associated with this document, representing the range of text selected by the user, or the current position of the caret.
No. MDN marks Document.getSelection() as Baseline Widely available (since November 2017). It is not Deprecated, Experimental, or Non-standard.
A Selection object, or null if the document has no browsing context — for example, the document of an iframe that is not attached to a document (MDN).
Call selection.toString(). Some functions like alert() call toString() automatically, but not all do — MDN recommends calling toString() explicitly when you need a string.
Yes. Window.getSelection() returns the Selection for the window's document and is the common alias of document.getSelection().
No. MDN: notice the difference between selection and focus. Document.activeElement returns the focused element.

Next: hasFocus()

Learn how document.hasFocus() tells you whether the document currently has focus.

hasFocus() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful