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

collapseToStart

Method of dom/Selectiondom/Selection

Syntax

var object = object.collapseToStart();

Return Value

Returns an object of type DOM NodeDOM Node

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Examples

The following code example puts the caret or insertion point at the beginning of the selected text.

<!DOCTYPE html>
<html>
<head>
   <title>Collapse to Start Example</title>
    <script>
        function SelectAtStart () {
            if (window.getSelection) {
                var selection = window.getSelection ();
                selection.collapseToStart ();
            }
        }
    </script>
</head>
<body>
<p>
    <div contenteditable="true" style="width:300px;">
        Select some text from this paragraph, and then click the following button.
        When you click the button, the caret, or insertion point, is set to the beginning of your selection.
    </div>
 </p>
 <p><input type="button" name="test" value="Set caret" onclick="SelectAtStart ()" /> </p>
</body>
</html>

Notes

Remarks

Raises an INVALID_STATE DOMException if there are no Ranges in the selection.

Syntax

Standards information

Attributions