All documentation ↓← All docs

editor.focus()

Moves keyboard focus into the editor so the user can start typing without clicking first. Use it after the editor is created, after inserting content programmatically, or after closing a dialog or side panel that took focus away from the editing area.

Syntax

editor.focus()

Parameters

None.

Returns

Nothing (undefined).

Behavior notes

focus() is not a thin wrapper around the native DOM focus call — it accounts for the editor's different modes and for browser selection quirks:

  • Code mode. While the editor is showing HTML source, focus is routed to the code editor rather than the rich-text area, so the call still does the right thing in either mode.
  • A selected control wins. If a control such as an image is currently selected, focus() returns without changing anything. This is deliberate: it prevents a programmatic focus call from silently dropping the user's image selection (and the resizing handles that go with it).
  • The caret is preserved. Some browsers discard the current selection when focus moves into the editing frame. The editor re-applies the stored range afterwards, so the caret stays where the user left it instead of jumping to the top of the document.

Example usage

Focus the editor as soon as it is ready, so the page is immediately typable:

Code example
var editor1 = new RichTextEditor("#div_editor1");
editor1.focus();

Insert a snippet, then return focus to the editor so typing continues naturally:

Code example
document.getElementById("btn_signature").onclick = function () {
    editor1.insertHTML("<p>-- <br/>Sent from RichTextEditor</p>");
    editor1.focus();   // the button took focus on click; give it back
};

Focus after revealing a hidden editor. Focusing an element that is still display:none does nothing, so reveal it first:

Code example
function openComposer() {
    document.getElementById("composer").style.display = "block";
    editor1.focus();
}

Related

Calling editor.focus() from your app

editor.focus() 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

Code example
var editor1 = new RichTextEditor("#div_editor1");
// The constructor is synchronous: the editor is ready on the next line.
editor1.focus();

React and Next.js

Code example
// editorRef is filled by the RichTextEditor wrapper from the React guides.
const editorRef = useRef(null);

function onClick() {
  editorRef.current?.focus();
}

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.current to null in between. Use editorRef.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.