This page is Almost Ready

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

collapse

Summary

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

Method of dom/Selectiondom/Selection

Syntax

 selObj.collapse(/* see parameter list */);

Parameters

parentNode

Data-type
DOM Node

The parent node that the selection is contained in.

offset

Data-type
Number

TODO: Verify these values, as Chrome seems to respect values other than 0 or 1 and act accordingly.

   0 - Collapses the selection from the anchor to the beginning of parentNode's text.
   1 - Collapses the selection from the anchor to the end of parentNode's text.

Return Value

No return value

Examples

Place the caret at the beginning of an HTML document’s body.

var body = document.getElementsByTagName('body')[0];
var selObj = window.getSelection();
selObj.collapse(body,0);// offset 0

Notes

Throws a WrongDocumentError DOMException if the parentNode is in another document. Throws a TypeError DOMException if the relevantly typed arguments are not specified.

Related specifications

HTML Editing APIs
W3C Community Group Work In Progress
HTML5 (Expired)
W3C Working Draft (Expired)

Attributions