A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.cdn.mozilla.net/en-US/docs/Web/API/Document/getSelection below:

Document: getSelection() method - Web APIs

Document: getSelection() method

Baseline Widely available

The getSelection() method of the Document interface returns the Selection object associated with this document, representing the range of text selected by the user, or the current position of the caret.

Syntax Parameters

None.

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).

Examples Getting a Selection object
const selection = document.getSelection();
const selRange = selection.getRangeAt(0);
// do stuff with the range

console.log(selection); // Selection object
String representation of the Selection object

Some functions (like Window.alert()) call toString() automatically and the returned value is passed to the function. As a consequence, this will return the selected text and not the Selection object:

However, not all functions call toString() automatically. To use a Selection object as a string, call its toString() method directly:

let selectedText = selection.toString();

You can call Window.getSelection(), which is identical to window.document.getSelection().

It is worth noting that currently getSelection() doesn't work on the content of <input> elements in Firefox. HTMLInputElement.setSelectionRange()) could be used to work around this.

Notice also the difference between selection and focus. Document.activeElement returns the focused element.

Specifications Browser compatibility

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4