execCommand method

bool execCommand(
  1. String commandId, [
  2. bool showUI,
  3. String value
])

The execCommand method implements multiple different commands. Some of them provide access to the clipboard, while others are for editing form inputs, contenteditable elements or entire documents (when switched to design mode).

To access the clipboard, the newer Clipboard API is recommended over execCommand(). However, there is no replacement for the editing commands: unlike direct DOM manipulation, modifications performed by execCommand() preserve the undo buffer (edit history).

Most commands affect the document's selection. For example, some commands (bold, italics, etc.) format the currently selected text, while others delete the selection, insert new elements (replacing the selection) or affect an entire line (indenting). Only the currently active editable element can be modified, but some commands (e.g. copy) can work without an editable element.

Note: Modifications performed by execCommand() may or may not trigger Element.beforeinput_event and Element.input_event events, depending on the browser and configuration. If triggered, the handlers for the events will run before execCommand() returns. Authors need to be careful about such recursive calls, especially if they call execCommand() in response to these events. From Firefox 82, nested execCommand() calls will always fail, see bug 1634262.

Implementation

external bool execCommand(
  String commandId, [
  bool showUI,
  String value,
]);