Selection
A Selection object represents the range of text selected by the user or the current position of the caret. Each document is associated with a unique selection object, which can be retrieved by document.getSelection() or window.getSelection() and then be examined and modified.
Properties
The Selection.anchorNode read-only property returns the Node in which the selection begins. It can return null if selection never existed in the document (e.g., an iframe that was never clicked on, or the node belongs to another document tree).
The Selection.anchorOffset read-only property returns the number of characters that the selection's anchor is offset within the Selection.anchorNode if said node is of type Text, CDATASection or Comment.
The Selection.focusOffset read-only property returns the number of characters that the selection's focus is offset within the Selection.focusNode if said node is of type Text, CDATASection or Comment.
The Selection.isCollapsed read-only property returns a boolean value which indicates whether or not there is currently any text selected. No text is selected when the selection's start and end points are at the same position in the content.
The Selection.rangeCount read-only property returns the number of ranges in the selection.
Functions
The Selection.collapseToEnd() method collapses the selection to the end of the last range in the selection. If the content of the selection is focused and editable, the caret will blink there.
The Selection.collapseToStart() method collapses the selection to the start of the first range in the selection. If the content of the selection is focused and editable, the caret will blink there.
The Selection.containsNode() method indicates whether a specified node is part of the selection.
The deleteFromDocument() method of the Selection interface invokes the Range.deleteContents() method on the selected Range.
The Selection.getComposedRanges() method returns an array of StaticRange objects representing the current selection ranges, and can return ranges that potentially cross shadow boundaries.
The getRangeAt() method of the Selection interface returns a range object representing a currently selected range.
The Selection.removeAllRanges() method removes all ranges from the selection, leaving the anchorNode and focusNode properties equal to null and nothing selected. When this method is called, a selectionchange event is fired at the document.
The Selection.removeRange() method removes a range from a selection.
The Selection.selectAllChildren() method adds all the children of the specified node to the selection. Previous selection is lost.
The setBaseAndExtent() method of the Selection interface sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.
The Selection.setPosition() method collapses the current selection to a single point. The document is not modified. If the content is focused and editable, the caret will blink there.