Documentation

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 Bold and bold behave the same.
  • Disabled commands are skipped. If a command is not currently enabled, execCommand() returns false without 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