editor.getSelection()
Returns the editor’s live selection object — the selection inside the editing area, not the outer page’s window.getSelection(). Use it to inspect what is selected, read ranges, or drive lower-level selection logic in custom commands.
Syntax
editor.getSelection()
Parameters
None.
Returns
The editor’s selection object.
Behavior notes
- Scoped to the editing iframe.This is the selection within the editor’s content, so its ranges point at nodes inside editor.getDocument()— not the surrounding page.
- It is the live selection. Read it at the moment you need it; it reflects wherever the caret or highlight currently is.
- For common needs there are higher-level helpers. Use getSelectedText() for the text and getSelectionElement() for the enclosing element rather than walking the selection by hand.
Example usage
Check whether anything is selected:
var editor1 = new RichTextEditor("#div_editor1");
var sel = editor1.getSelection();
if (sel && !sel.isCollapsed) {
// there is a highlighted range to act on
}Read the current range from the selection:
var sel = editor1.getSelection();
if (sel && sel.rangeCount) {
var range = sel.getRangeAt(0);
console.log(range.toString());
}Related
- editor.getSelectedText()— the plain text of the selection.
- editor.getSelectionElement()— the element that contains the selection.
- editor.collapse(bStart)— collapse the selection to a caret.