docs.rodeo

MDN Web Docs mirror

Selection

{{ApiRef("Selection API")}} 

A Selection object represents the range of text selected by the user or the current position of the caret. Each {{domxref("document")}}  is associated with a unique selection object, which can be retrieved by {{DOMxRef("document.getSelection()")}}  or {{domxref("window.getSelection()")}}  and then be examined and modified.

A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection and the focus is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button, and the focus is placed where you released the mouse button.

Note: Anchor and focus should not be confused with the start and end positions of a selection. The anchor can be placed before the focus or vice versa, depending on the direction you made your selection.

Instance properties

Instance methods

Notes

String representation of a selection

Calling the {{DOMxRef("Selection.toString()")}}  method returns the text contained within the selection, e.g.:

const selObj = window.getSelection();
window.alert(selObj);

Note that using a selection object as the argument to window.alert will call the object’s toString method.

Multiple ranges in a selection

A selection object represents the {{DOMxRef("Range")}} s that the user has selected. Typically, it holds only one range, accessed as follows:

const selObj = window.getSelection();
const range = selObj.getRangeAt(0);

As the Selection API specification notes, the Selection API was initially created by Netscape and allowed multiple ranges (for instance, to allow the user to select a column from a {{HTMLElement("table")}} ). However, browsers other than Gecko did not implement multiple ranges, and the specification also requires the selection to always have a single range.

Selection and input focus

Selection and input focus (indicated by {{DOMxRef("Document.activeElement")}} ) have a complex relationship that varies by browser. In cross-browser compatible code, it’s better to handle them separately.

Safari and Chrome (unlike Firefox) currently focus the element containing selection when modifying the selection programmatically; it’s possible that this may change in the future (see W3C bug 14383 and WebKit bug 38696).

Behavior of Selection API in terms of editing host focus changes

The Selection API has a common behavior (i.e., shared between browsers) that governs how focus behavior changes for editing hosts after certain methods are called.

The behavior is as follows:

  1. An editing host gains focus if the previous selection was outside of it.
  2. A Selection API method is called, causing a new selection to be made with the selection range inside the editing host.
  3. Focus then moves to the editing host.

[!NOTE] The Selection API methods may only move focus to an editing host, not to other focusable elements (e.g., {{HTMLElement("a")}} ).

The above behavior applies to selections made using the following methods:

And when the {{DOMxRef("Range")}}  is modified using the following methods:

Glossary

Other key terms used in this section.

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN