JavaScript Document execCommand() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.execCommand(commandName, showDefaultUI, valueArgument) runs a named editing or clipboard command on the current selection — usually inside a contenteditable region or a form field.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.execCommand. Start with this line.

JavaScript
document.execCommand(name, showUI, value)
CallWhat it does
document.execCommand(name, showUI, value)Primary signature.
document.execCommand(...)Call with the arguments your case needs.

Built into the browser. Call execCommand(...) on document — no import required.

Library declarations
JavaScript
document.execCommand(name, showUI, value)

Parameters

ParameterDescription
commandNameRequired. Command id, e.g. "bold", "insertText"
showDefaultUIRequired. Whether to show a default UI (often unused; pass false)

Return Value

Return ValueDescription
*false if unsupported or disabled

JavaScript Examples

Example 1 — Bold a Selection

Focus a contenteditable region, select all, then run bold.

JavaScript
const editor = document.getElementById("editor"); // contenteditable
editor.focus();
document.execCommand("selectAll", false, null);
const ok = document.execCommand("bold", false, null);

console.log(ok); // true or false
console.log(editor.innerHTML); // often wraps text in <b> or <strong>
Try It Yourself

How It Works

  1. Focus. Commands apply to the active editable element.
  2. Select. bold formats the current selection.
  3. Run the command. Second argument is usually false.
  4. Check ok. false means unsupported or blocked.

Example 2 — insertText

Insert plain text at the caret. MDN notes this path can help preserve undo history.

JavaScript
const editor = document.getElementById("editor"); // contenteditable
editor.focus();
document.execCommand("selectAll", false, null);
const ok = document.execCommand("insertText", false, "Hello");

console.log(ok);
console.log(editor.textContent); // "Hello"
Try It Yourself

How It Works

  1. Focus the editor. Insertion needs an active editable target.
  2. Replace selection. selectAll then insert replaces the whole content.
  3. Pass the text. The third argument is the string to insert.
  4. Undo-friendly. Unlike some direct DOM edits, this may stay in the undo stack.

Example 3 — Probe with queryCommandSupported

Check whether a command name is recognized before calling it.

JavaScript
console.log(document.queryCommandSupported("bold")); // usually true
console.log(document.queryCommandSupported("insertText")); // usually true
console.log(document.queryCommandSupported("not-a-real-command")); // false
Try It Yourself

How It Works

  1. Ask first. queryCommandSupported reports recognition, not success of a future call.
  2. Common commands. bold / insertText are widely known.
  3. Fake names. Unknown command strings return false.
  4. Still test. Supported ≠ always enabled for the current selection/gesture.

Browser Support

Document.execCommand() still exists in many browsers for compatibility, but MDN marks it Deprecated and Non-standard. Support for individual commands varies.

✓ Deprecated · Non-standard

Document.execCommand()

Present in Chrome, Firefox, Safari, Edge, and Opera for legacy editing. Do not rely on full cross-browser command parity.

Legacy Avoid for new work
Google Chrome Legacy support (varies by command)
Partial
Microsoft Edge Legacy support (varies by command)
Partial
Mozilla Firefox Legacy support (varies by command)
Partial
Apple Safari Legacy support (varies by command)
Partial
Opera Legacy support (varies by command)
Partial
Internet Explorer Legacy
Partial
execCommand() Legacy / inconsistent

Bottom line: Literacy and rare undo-buffer cases only. See the MDN execCommand() reference. Prefer the Clipboard API and modern editors for new apps.

Frequently Asked Questions

It runs an editing or clipboard-related command — for example bold/italic on a selection, insertText into a contenteditable region, or legacy copy/cut/paste.
Yes. MDN marks Document.execCommand() as Deprecated and Non-standard. It is not part of any current specification and is not recommended for new projects.
MDN notes some cases still lack full alternatives — for example execCommand can preserve the undo buffer (edit history). If you use it, test cross-browser and consider queryCommandSupported().
Prefer the Clipboard API (for example navigator.clipboard) over execCommand copy/cut/paste for new work.
It returns false if the command is unsupported or disabled. true usually requires a user interaction — you cannot rely on the return value alone to probe support before a gesture.
beforeinput and input may or may not fire depending on the browser. Nested execCommand calls can fail in some engines (for example Firefox 82+).

Next: exitFullscreen()

Learn how to leave fullscreen mode with document.exitFullscreen().

exitFullscreen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful