document.getSelection() is an instance method that returns the document’s Selection object — the text the user highlighted, or the caret position (see MDN Document: getSelection()). Learn how to read selected text, work with ranges, and how selection differs from focus.
01
Kind
Instance method
02
Args
None
03
Returns
Selection / null
04
Text
toString()
05
Alias
window.getSelection
06
Status
Baseline
Fundamentals
Introduction
When a visitor highlights words on a page — or places the caret in editable text — the browser tracks that as a selection. document.getSelection() is how JavaScript reads it.
MDN: the method returns the Selection object associated with this document, representing the range of text selected by the user, or the current position of the caret.
💡
Think: “what did the user highlight?”
1) Call document.getSelection() 2) Get a Selection object (or null) 3) Read text with selection.toString() 4) Or dig into ranges with getRangeAt(0)
⚠️
Selection is not focus (MDN)
MDN reminds you that selection and focus are different. Document.activeElement returns the focused element — not the highlighted text. A button can be focused while no text is selected.
An instance method on the Document interface (MDN). It takes no arguments and returns the current selection state for that document.
No parameters — call it with empty parentheses (MDN).
Return value — a Selection object, or null without a browsing context (MDN).
Selected text — use selection.toString() for a string (MDN).
Ranges — selection.getRangeAt(0) for the first range (MDN example).
Window alias — Window.getSelection() is identical to window.document.getSelection() (MDN).
Input caveat — MDN: currently getSelection() doesn’t work on the content of <input> elements in Firefox; HTMLInputElement.setSelectionRange() can help.
Foundation
📝 Syntax
General form of Document.getSelection (MDN):
JavaScript
getSelection()
Parameters
None (MDN).
Return value
A Selection object, or null if the document has no browsing context — for example, it is the document of an <iframe> that is not attached to a document (MDN).
MDN quick sample
JavaScript
const selection = document.getSelection();
const selRange = selection.getRangeAt(0);
// do stuff with the range
console.log(selection); // Selection object
let selectedText = selection.toString();
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 user-highlighted text and caret position across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getSelection()Wide
Bottom line: Use getSelection to read highlighted text and caret ranges. Remember it differs from focus (activeElement), and watch input quirks in Firefox.
Wrap Up
Conclusion
document.getSelection() returns the document’s Selection object so you can read highlighted text, inspect ranges, or react to the caret. Prefer explicit toString(), and remember selection is not the same as focus.
Handle null for documents without a browsing context (MDN)
Use activeElement when you care about focus (MDN)
Test form-field selection separately (Firefox input note on MDN)
❌ Don’t
Assume every API auto-stringifies a Selection (MDN)
Confuse selection with activeElement focus
Call getRangeAt(0) when rangeCount is 0
Expect identical <input> behavior in every browser (MDN)
Forget window.getSelection() is the same API (MDN)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getSelection()
Read the user’s text selection and caret.
5
Core concepts
📝01
Returns
Selection
MDN
🔄02
Text
toString()
MDN
🎯03
Ranges
getRangeAt
MDN
⚡04
≠ focus
activeElement
MDN
🛡05
Status
Baseline
2017
❓ 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. MDN: Window.getSelection() is identical to window.document.getSelection().
No. MDN: notice the difference between selection and focus. Document.activeElement returns the focused element.
Did you know?
MDN points out that alert(selection) often shows the selected text because alert calls toString() for you — but many other APIs will not, so explicit selection.toString() is the safer habit.