AwaitedDOM/ Selection

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Properties

.anchorNode
W3C

Returns the Node in which the selection begins. Can return null if selection never existed in the document (e.g., an iframe that was never clicked on).

Type: SuperNode

.anchorOffset
W3C

Returns a number representing the offset of the selection's anchor within the anchorNode. If anchorNode is a text node, this is the number of characters within anchorNode preceding the anchor. If anchorNode is an element, this is the number of child nodes of the anchorNode preceding the anchor.

Type: Promise<number>

.focusNode
W3C

Returns the Node in which the selection ends. Can return null if selection never existed in the document (for example, in an iframe that was never clicked on).

Type: SuperNode

.focusOffset
W3C

Returns a number representing the offset of the selection's anchor within the focusNode. If focusNode is a text node, this is the number of characters within focusNode preceding the focus. If focusNode is an element, this is the number of child nodes of the focusNode preceding the focus.

Type: Promise<number>

.isCollapsed
W3C

Returns a Boolean indicating whether the selection's start and end points are at the same position.

Type: Promise<boolean>

.rangeCount
W3C

Returns the number of ranges in the selection.

Type: Promise<number>

.type
W3C

Returns a string describing the type of the current selection.

Type: Promise<string>

Methods

.addRange(range)
W3C

A Range object that will be added to the selection.

Arguments:

  • range Range. A Range object that will be added to the Selection.

Returns: Promise<void>

.collapse(node, offset?)
W3C

Collapses the current selection to a single point.

Arguments:

  • node Node. The caret location will be within this node. This value can also be set to null — if null is specified, the method will behave like Selection.removeAllRanges(), i.e. all ranges will be removed from the selection.
  • offset number. The offset in node to which the selection will be collapsed. If not specified, the default value 0 is used.

Returns: Promise<void>

.collapseToEnd()
W3C

Collapses the selection to the end of the last range in the selection.

Returns: Promise<void>

.collapseToStart()
W3C

Collapses the selection to the start of the first range in the selection.

Returns: Promise<void>

.containsNode(node, allowPartialContainment?)
W3C

Indicates if a certain node is part of the selection.

Arguments:

  • node Node. The node that is being looked for in the selection.
  • allowPartialContainment boolean. When true, containsNode() returns true when a part of the node is part of the selection. When false, containsNode() only returns true when the entire node is part of the selection. If not specified, the default value false is used.

Returns: Promise<boolean>

.deleteFromDocument()
W3C

Deletes the selection's content from the document.

Returns: Promise<void>

.empty()
W3C

Removes all ranges from the selection. This is an alias for removeAllRanges() — See Selection.removeAllRanges() for more details.

Returns: Promise<void>

.extend(node, offset?)
W3C

Moves the focus of the selection to a specified point.

Arguments:

  • node Node. The node within which the focus will be moved.
  • offset number. The offset position within node where the focus will be moved to. If not specified, the default value 0 is used.

Returns: Promise<void>

.getRangeAt(index)
W3C

Returns a Range object representing one of the ranges currently selected.

Arguments:

  • index number. The zero-based index of the range to return. A negative number or a number greater than or equal to Selection.rangeCount will result in an error.

Returns: Range

.modify(alter, direction, granularity)
W3C

Changes the current selection.

Arguments:

  • alter string. The type of change to apply. Specify "move" to move the current cursor position or "extend" to extend the current selection.
  • direction string. The direction in which to adjust the current selection. You can specify "forward" or "backward" to adjust in the appropriate direction based on the language at the selection point. If you want to adjust in a specific direction, you can specify "left" or "right".
  • granularity string. The distance to adjust the current selection or cursor position. You can move by "character", "word", "sentence", "line", "paragraph", "lineboundary", "sentenceboundary", "paragraphboundary", or "documentboundary".

Returns: Promise<void>

.removeAllRanges()
W3C

Removes all ranges from the selection.

Returns: Promise<void>

.removeRange(range)
W3C

Removes a range from the selection.

Arguments:

  • range Range. A range object that will be removed to the selection.

Returns: Promise<void>

.selectAllChildren(node)
W3C

Adds all the children of the specified node to the selection.

Arguments:

  • node Node. All children of parentNode will be selected. parentNode itself is not part of the selection.

Returns: Promise<void>

.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset)
W3C

Sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.

Arguments:

  • anchorNode Node. The node at the start of the selection.
  • anchorOffset number. The number of child nodes from the start of the anchor node that should be excluded from the selection. So for example, if the value is 0 the whole node is included. If the value is 1, the whole node minus the first child node is included. And so on.
  • focusNode Node. The node at the end of the selection.
  • focusOffset number. The number of child nodes from the start of the focus node that should be included in the selection. So for example, if the value is 0 the whole node is excluded. If the value is 1, the first child node is included. And so on.

Returns: Promise<void>

.setPosition(node, offset?)
W3C

Collapses the current selection to a single point. This is an alias for collapse() — See Selection.collapse() for more details.

Arguments:

  • node Node. Needs content.
  • offset number. Needs content.

Returns: Promise<void>

.toString()
W3C

Returns a string currently being represented by the selection object, i.e. the currently selected text.

Returns: Promise<string>

Edit this page on GitHub