This page is In Progress

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Selection

Summary

Represents the text selection in the greater page, possibly spanning multiple elements, when the user drags over static text and other parts of the page.

Overview

Selection is the class of the object returned by window.getSelection() and other methods. For information about text selection in an individual text editing element, see Input, TextArea and document.activeElement which typically return the parent object returned from window.getSelection().

Properties

anchorNode
Returns the element or node that contains the start of the selection.
anchorOffset
Returns the number of characters that the selection’s anchor is offset within the anchorNode.
focusNode
Retrieves the element or node that contains the end of a selection.
focusOffset
Returns the number of characters that the selection’s focus is offset within the focusNode.
isCollapsed
Retrieves whether a selection is collapsed or empty.
rangeCount
Returns the number of ranges in a selection

typeDetail
:

Methods

addRange

Adds a Range to the current selection.

clear

The clear() method is not in HTMLSelection Object.

Use the removeAllRanges Method instead.

collapse

Replaces the current selection with an empty selection (or a caret) at the given offset.

collapseToEnd

Collapses or sets the insertion point or caret at the end of a selection object.

If the content the selection is in is focused and editable, the caret will blink there.

collapseToStart
:

createRange
:

createRangeCollection
:

deleteFromDocument
Deletes the selected nodes from a document.
getRangeAt
Returns a specified Range from a selection. The Range is specified by an index and cannot be greater than the number that is returned by rangeCount.
removeAllRanges
Removes all ranges from a selection.
removeRange
Removes a range from a selection.
selectAllChildren
Adds all the children of the specified node to the selection. Previous selection is lost.

Events

No events.

Examples

A selection object represents the ranges that the user has selected. Typically, it holds only one range, accessed as follows:

var selObj = window.getSelection();
if(selObj.rangeCount){ range  = selObj.getRangeAt(0);}

Related specifications

W3C HTML5
Proposed Recommendation

Attributions