HTMLSelection object

Represents the active selection, which is typically a highlighted block of text or other elements in the document. The active selection can be used to complete an action issued by a user or a script.

HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1Internet Explorer 9

 

Members

The HTMLSelection object has these types of members:

Methods

The HTMLSelection object has these methods.

MethodDescription
addRange

Adds a Range to the current selection.

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.

collapseToStart

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

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

Replaces the current selection with all the contents of the given node.

 

Properties

The HTMLSelection object has these properties.

PropertyDescription

anchorNode

Returns the element or node that contains the start of the selection.

anchorOffset

Retrieves the starting position of a selection that is relative to the anchorNode.

focusNode

Retrieves the element or node that contains the end of a selection.

focusOffset

Retrieves the end position of a selection that is relative to the focusNode.

isCollapsed

Retrieves whether a selection is collapsed or empty.

rangeCount

Returns the number of ranges in a selection

 

Standards information

Remarks

You can use the HTMLSelection object as input from the user to identify which portion of the document to act on, or as output to the user to show the results of an action.

Users and scripts can create selections. Users create selections by dragging a finger or mouse over a portion of the document. Scripts create selections by calls to selection methods. To get the active selection, apply the getSelection method to the document object.

A document can have only one selection at a time. Use the isCollapsed property to test whether a selection is empty. Although an empty selection contains nothing, you can use it to mark a position, or place a caret in the document.

 

 

Show:
© 2014 Microsoft