This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
When an HTML document has been switched to
document object exposes an
method to run commands that manipulate the current editable region, such as form inputs or
Most commands affect the document's selection (bold, italics, etc.), while others
insert new elements (adding a link), or affect an entire line (indenting). When using
execCommand() affects the currently active
Note: This method is obsolete and should not be used. In particular, to interact with the clipboard, consider using the Clipboard API instead.
document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
Boolean that is
false if the command is unsupported or
document.execCommand() only returns
true if it is invoked as part of a user interaction. You can't use it to
verify browser support before calling a command. From Firefox 82, nested
document.execCommand() calls will always return
DOMStringspecifying the name of the command to execute. See Commands for a list of possible commands.
Booleanindicating whether the default user interface should be shown. This is not implemented in Mozilla.
- For commands which require an input argument, is a
DOMStringproviding that information. For example,
insertImagerequires the URL of the image to insert. Specify
nullif no argument is needed.
- Changes the document background color. In
styleWithCssmode, it affects the background color of the containing block instead. This requires a
<color>value string to be passed in as a value argument. Note that Internet Explorer uses this to set the text background color.
- Toggles bold on/off for the selection or at the insertion point. Internet Explorer
<strong>tag instead of
- Clears all authentication credentials from the cache.
- Makes the content document either read-only or editable. This requires a boolean true/false as the value argument. (Not supported by Internet Explorer.)
- Copies the current selection to the clipboard. Conditions of having this behavior enabled vary from one browser to another, and have evolved over time. Check the compatibility table to determine if you can use it in your case.
- Creates an hyperlink from the selection, but only if there is a selection. Requires
a URI string as a value
argument for the hyperlink's
href. The URI must contain at least a single character, which may be whitespace. (Internet Explorer will create a link with a
- Removes the current selection and copies it to the clipboard. When this behavior is enabled varies between browsers, and its conditions have evolved over time. Check the compatibility table for usage details.
- Adds a
<small>tag around the selection or at the insertion point. (Not supported by Internet Explorer.)
- Changes the paragraph separator used when new paragraphs are created in editable text regions. See Differences in markup generation for more details.
- Deletes the current selection.
- Enables or disables the grabber that allows absolutely-positioned elements to be moved around. The grabber is disabled by default since Firefox 64 (bug 1490641).
- Enables or disables the table row/column insertion and deletion controls. The controls are disabled by default since Firefox 64 (bug 1490641).
- Enables or disables the resize handles on images, tables, and absolutely-positioned elements and other resizable objects. The handles are disabled by default since Firefox 64 (bug 1490641).
- Changes the font name for the selection or at the insertion point. This requires a
font name string (like
"Arial") as a value argument.
- Changes the font size for the selection or at the insertion point. This requires an
7as a value argument.
- Changes a font color for the selection or at the insertion point. This requires a hexadecimal color value string as a value argument.
- Adds an HTML block-level element around the line containing the current selection,
replacing the block element containing the line if one exists (in Firefox,
<blockquote>is the exception — it will wrap any containing block element). Requires a tag-name string as a value argument. Virtually all block-level elements can be used. (Internet Explorer and Edge support only heading tags
PRE, which must be wrapped in angle brackets, such as
- Deletes the character ahead of the cursor's position, identical to hitting the Delete key on a Windows keyboard.
- Adds a heading element around a selection or insertion point line. Requires the
tag-name string as a value argument (i.e.
"H6"). (Not supported by Internet Explorer and Safari.)
- Changes the background color for the selection or at the insertion point. Requires a
color value string as a value argument.
truefor this to function. (Not supported by Internet Explorer.)
- Adds a
<big>tag around the selection or at the insertion point. (Not supported by Internet Explorer.)
- Indents the line containing the selection or insertion point. In Firefox, if the selection spans multiple lines at different levels of indentation, only the least indented lines in the selection will be indented.
- Controls whether the Enter key inserts a
<br>element, or splits the current block element into two. (Not supported by Internet Explorer.)
- Inserts a
<hr>element at the insertion point, or replaces the selection with it.
- Inserts an HTML string at the insertion point (deletes selection). Requires a valid HTML string as a value argument. (Not supported by Internet Explorer.)
- Inserts an image at the insertion point (deletes selection). Requires a URL string
for the image's
srcas a value argument. The requirements for this string are the same as
- Creates a numbered ordered list for the selection or at the insertion point.
- Creates a bulleted unordered list for the selection or at the insertion point.
- Inserts a paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.)
- Inserts the given plain text at the insertion point (deletes selection).
- Toggles italics on/off for the selection or at the insertion point. (Internet
Explorer uses the
<em>element instead of
- Centers the selection or insertion point.
- Justifies the selection or insertion point.
- Justifies the selection or insertion point to the left.
- Right-justifies the selection or the insertion point.
- Outdents the line containing the selection or insertion point.
- Pastes the clipboard contents at the insertion point (replaces current selection). Disabled for web content.
- Redoes the previous undo command.
- Removes all formatting from the current selection.
- Selects all of the content of the editable region.
- Toggles strikethrough on/off for the selection or at the insertion point.
- Toggles subscript on/off for the selection or at the insertion point.
- Toggles superscript on/off for the selection or at the insertion point.
- Toggles underline on/off for the selection or at the insertion point.
- Undoes the last executed command.
- Removes the anchor element from a selected hyperlink.
- Toggles the use of HTML tags or CSS for the generated markup. Requires a boolean true/false as a value argument.
- NOTE: This argument is logically backwards (i.e. use
falseto use CSS,
trueto use HTML) and unsupported by Internet Explorer. This has been deprecated in favor of
- Replaces the
styleattributes in markup, false generates presentational elements.
An example of how to use it on CodePen.
This feature is not part of any current specification. It is no longer on track to become a standard.
BCD tables only load in the browser