When an HTML document has been switched to designMode, its document object exposes an execCommand method to run commands that manipulate the current editable region, such as form inputs or contentEditable elements.
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 contentEditable, execCommand() affects the currently active editable element.
bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
A Boolean that is false if the command is unsupported or disabled.
Note: Only returns true if part of a user interaction. Don't try using the return value to verify browser support before calling a command.
aCommandNameDOMString specifying the name of the command to execute. See Commands for a list of possible commands.aShowDefaultUIBoolean indicating whether the default user interface should be shown. This is not implemented in Mozilla.aValueArgumentDOMString providing that information. For example, insertImage requires the URL of the image to insert. Specify null if no argument is needed.backColorstyleWithCss mode, 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.bold<strong> tag instead of <b>.ClearAuthenticationCachecontentReadOnlycopycreateLinkhref. The URI must contain at least a single character, which may be whitespace. (Internet Explorer will create a link with a null value.)cutdecreaseFontSize<small> tag around the selection or at the insertion point. (Not supported by Internet Explorer.)defaultParagraphSeparatordeleteenableAbsolutePositionEditorenableInlineTableEditingenableObjectResizingfontName"Arial") as a value argument.fontSize1-7 as a value argument.foreColorformatBlock<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 H1–H6, ADDRESS, and PRE, which must be wrapped in angle brackets, such as "<H1>".)forwardDeleteheading"H1", "H6"). (Not supported by Internet Explorer and Safari.)hiliteColoruseCSS must be true for this to function. (Not supported by Internet Explorer.)increaseFontSize<big> tag around the selection or at the insertion point. (Not supported by Internet Explorer.)indentinsertBrOnReturn<br> element, or splits the current block element into two. (Not supported by Internet Explorer.)insertHorizontalRule<hr> element at the insertion point, or replaces the selection with it.insertHTMLinsertImagesrc as a value argument. The requirements for this string are the same as createLink.insertOrderedListinsertUnorderedListinsertParagraphinsertTextitalic<em> element instead of <i>.)justifyCenterjustifyFulljustifyLeftjustifyRightoutdentpasteredoremoveFormatselectAllstrikeThroughsubscriptsuperscriptunderlineundounlinkuseCSS
false to use CSS, true to use HTML) and unsupported by Internet Explorer. This has been deprecated in favor of styleWithCSS.styleWithCSSuseCSS command. true modifies/generates style attributes in markup, false generates presentational elements.An example of how to use it on CodePen.
| Specification | Status | Comment |
|---|---|---|
| HTML Editing APIs The definition of 'execCommand' in that specification. | Editor's Draft | Initial definition |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
insertBrOnReturn command
|
No | No | Yes | No | No | No |
copy command
|
Yes | Yes | 41 | 9 | 29 | 10 |
cut command
|
Yes | Yes | 41 | 9 | 29 | 10 |
defaultParagraphSeparator command
|
Yes | Yes | 55 | No | Yes | No |
ClearAuthenticationCache command
|
No | Yes | No | 6 | No | No |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | Yes | Yes | Yes | Yes | Yes | ? |
insertBrOnReturn command
|
No | No | No | Yes | No | No | ? |
copy command
|
Yes | Yes | Yes | 41 | 29 | 10 | ? |
cut command
|
Yes | Yes | Yes | 41 | 29 | 10 | ? |
defaultParagraphSeparator command
|
Yes | Yes | Yes | 55 | Yes | No | ? |
ClearAuthenticationCache command
|
No | No | Yes | No | No | No | ? |
HTMLElement.contentEditabledocument.designModedocument.execCommand.?
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand