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.
aCommandName
DOMString
specifying the name of the command to execute. See Commands for a list of possible commands.aShowDefaultUI
Boolean
indicating whether the default user interface should be shown. This is not implemented in Mozilla.aValueArgument
DOMString
providing that information. For example, insertImage
requires the URL of the image to insert. Specify null
if no argument is needed.backColor
styleWithCss
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>
.ClearAuthenticationCache
contentReadOnly
copy
createLink
href
. The URI must contain at least a single character, which may be whitespace. (Internet Explorer will create a link with a null
value.)cut
decreaseFontSize
<small>
tag around the selection or at the insertion point. (Not supported by Internet Explorer.)defaultParagraphSeparator
delete
enableAbsolutePositionEditor
enableInlineTableEditing
enableObjectResizing
fontName
"Arial"
) as a value argument.fontSize
1
-7
as a value argument.foreColor
formatBlock
<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>"
.)forwardDelete
heading
"H1"
, "H6"
). (Not supported by Internet Explorer and Safari.)hiliteColor
useCSS
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.)indent
insertBrOnReturn
<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.insertHTML
insertImage
src
as a value argument. The requirements for this string are the same as createLink
.insertOrderedList
insertUnorderedList
insertParagraph
insertText
italic
<em>
element instead of <i>
.)justifyCenter
justifyFull
justifyLeft
justifyRight
outdent
paste
redo
removeFormat
selectAll
strikeThrough
subscript
superscript
underline
undo
unlink
useCSS
false
to use CSS, true
to use HTML) and unsupported by Internet Explorer. This has been deprecated in favor of styleWithCSS
.styleWithCSS
useCSS
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.contentEditable
document.designMode
document.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