editor.collapse(bStart)
Collapses the current selection down to a single insertion point (a caret with no highlighted text). Use it after a programmatic selection or a command that acted on a range, when you want to place the caret at one edge of what was selected rather than leave the whole range highlighted.
Syntax
editor.collapse(bStart)
Parameters
bStart — boolean. true collapses the selection to its start, false to its end. Omitting it behaves like false (collapse to the end).
Returns
Nothing (undefined).
Behavior notes
- Already-collapsed selections are left alone. If the selection is already a caret,
collapse()returns immediately without moving it. - No active range selects the document first. If there is no selection range at all (for example the editor has not been focused yet),
collapse()falls back to selecting the whole document, so the call still leaves a well-defined selection to work from. - It operates on the live selection,the same one the user sees — there is no separate caret to keep in sync.
Example usage
Apply a command to the whole document, then drop the caret at the end so typing continues after it:
var editor1 = new RichTextEditor("#div_editor1");
editor1.setHTMLCode("<b>hello world</b>");
editor1.selectDoc();
editor1.execCommand("forecolor", "red");
editor1.collapse(false); // caret lands after the colored textCollapse to the start instead, to position the caret before the selection:
editor1.selectDoc();
editor1.collapse(true); // caret lands at the very beginningRelated
- editor.selectDoc()— select the whole document (often paired with collapse).
- editor.getSelection()— read the current selection.
- editor.focus()— move keyboard focus into the editor.