editor.execCommand(command, value)
Runs an editor command by name against the current selection — the same commands the toolbar buttons invoke. It is the main way to drive the editor from your own buttons, keyboard shortcuts, or automation: formatting (bold, forecolor), insertion (insertimage, inserttable), view toggles (fullscreenenter), and more.
Syntax
editor.execCommand(command, value)
Parameters
command — the command name (case-insensitive). See all commands for the full list.
value — optional. Some commands need a value — for example forecolor takes a color, and formatblock takes a tag such as h2.
Returns
false when the command is disabled or blocked; otherwise the command runs.
Behavior notes
- Command names are case-insensitive. They are lowercased internally, so
Boldandboldbehave the same. - Disabled commands are skipped. If a command is not currently enabled,
execCommand()returnsfalsewithout acting — check isCommandEnabled() first if you need to know in advance. - Every command is interceptable. Before running, the editor fires an exec_command event (and a per-command
exec_command_<name>event); returning a value from your handler overrides the built-in behavior.
Example usage
Toggle a view command:
var editor1 = new RichTextEditor("#div_editor1");
editor1.execCommand("fullscreenenter");Run a command that needs a value:
editor1.execCommand("forecolor", "red");
editor1.execCommand("formatblock", "h2");Wire a custom button to a formatting command:
document.getElementById("btn_bold").onmousedown = function (e) {
e.preventDefault(); // keep focus in the editor
editor1.execCommand("bold");
};Related
- All commands— the full list of command names.
- editor.isCommandEnabled(command)— whether a command can run right now.
- editor.isCommandActive(command)— whether a toggle command is currently on.
- exec_command event— intercept or override commands.
Calling editor.execCommand() from your app
editor.execCommand() 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.execCommand(command, value);React and Next.js
// editorRef is filled by the RichTextEditor wrapper from the React guides.
const editorRef = useRef(null);
function onClick() {
editorRef.current?.execCommand(command, value);
}
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.