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.
Calling editor.getSelection() from your app
editor.getSelection() belongs to the editor instance. Every integration below works the same way: keep the object that new RichTextEditor(...) returns, and call it on that object.
Plain JavaScript
var editor1 = new RichTextEditor("#div_editor1");
// The constructor is synchronous: the editor is ready on the next line.
editor1.getSelection();React and Next.js
// editorRef is filled by the RichTextEditor wrapper from the React guides.
const editorRef = useRef(null);
function onClick() {
editorRef.current?.getSelection();
}
return <RichTextEditor editorRef={editorRef} />;In Vue and Angular, the wrapper components in the guides keep the instance in a component variable (editor), so the call is the same. See the React, Vue and Angular pages, or download a tested project from the guides.
If the editor is undefined
- The call ran before the component mounted. Call it from an event handler or after the mount effect has run.
- In React development mode, Strict Mode mounts, unmounts and mounts again, so the wrapper sets
editorRef.currenttonullin between. UseeditorRef.current?.rather than storing the instance elsewhere. - The editor was destroyed (
editor.destroy()) when its component unmounted.
All methods: method reference · all events: event reference.