document.execCommand() is a deprecated, non-standard instance method that runs editing and clipboard commands (see MDN Document: execCommand()). Learn the three parameters, common commands like bold and insertText, return-value pitfalls, undo notes, modern alternatives, and five try-it labs.
01
Kind
Instance method
02
Args
command, UI, value
03
Returns
boolean
04
Targets
selection / editable
05
Status
Deprecated
06
Also
Non-standard
Fundamentals
Introduction
Before today’s editor libraries and Clipboard API, browsers exposed a single switchboard: document.execCommand(commandName, showDefaultUI, valueArgument). One call could bold selected text, insert HTML, or try to copy to the clipboard.
MDN: commands mainly affect the current selection inside an active editable control — a contenteditable region, a form field, or a document in designMode. Some commands (like copy) can work without an editable element.
💡
Think: legacy rich-text remote control
1) Focus an editable area and select text (if needed) 2) Call execCommand("bold", false, null) (or another command) 3) Check the boolean return value 4) Prefer Clipboard API / modern editors for new products
commandName — string specifying the command to run (MDN). Common beginner ones: bold, italic, underline, insertText, undo, redo, copy.
showDefaultUI — boolean; show a default UI if the browser has one. MDN: not implemented in Mozilla. Pass false in tutorials.
valueArgument — extra string for commands that need data (font name, color, HTML, URL, …). Pass null when none is needed (MDN).
Return value
A boolean that is false if the command is unsupported or disabled (MDN). Important: MDN notes execCommand only returns true when invoked as part of a user interaction — you cannot rely on a cold call to prove browser support.
Beginner-friendly commands (subset of MDN list)
Command
What it does
Value?
bold / italic / underline
Toggle formatting on the selection
No (null)
insertText
Insert plain text (replaces selection)
Yes — the text
insertHTML
Insert HTML (XSS risk — prefer Trusted Types)
Yes — markup
createLink
Wrap selection in a link
Yes — href URI
foreColor / hiliteColor
Text / highlight color
Yes — color string
undo / redo
Walk the edit history
No
copy / cut / paste
Clipboard (legacy; prefer Clipboard API)
Usually no
removeFormat
Strip formatting from the selection
No
MDN-style insertText call
JavaScript
textarea.focus();
const ok = document.execCommand("insertText", false, "");
if (!ok) {
console.error("insertText failed or unsupported");
}
Compare
⚖️ execCommand vs Clipboard API vs DOM edits
Approach
Strength
Typical use
execCommand
Legacy commands; may keep undo history (MDN)
Old toolbars; rare undo-preserving inserts
Clipboard API
Modern, permission-aware clipboard
Copy / paste in new apps (MDN recommendation)
Direct DOM / editor libs
Full control, clearer security model
New rich-text products (ProseMirror, TipTap, …)
Cheat Sheet
⚡ Quick Reference
Goal
Code
Bold selection
document.execCommand("bold", false, null)
Insert text
document.execCommand("insertText", false, "Hi")
Undo
document.execCommand("undo", false, null)
Probe a command
document.queryCommandSupported("bold")
Modern copy
await navigator.clipboard.writeText(text)
MDN status
Deprecated & Non-standard
Snapshot
🔍 At a Glance
Four facts about document.execCommand().
Returns
boolean
success-ish
Needs
user gesture
for true
Clipboard?
Clipboard API
prefer
Status
Deprecated
+ Non-standard
Compare
📋 When MDN still mentions execCommand
Prefer modern API
Legacy / niche gap
Clipboard
Clipboard API (MDN)
copy / cut / paste commands
Formatting
Editor libraries / DOM + CSS
Old bold / italic toolbars
Undo history
Editor-controlled history
MDN: execCommand can preserve browser undo
New products
Always prefer standards
Only after testing + feature checks
Hands-On
Examples Gallery
Examples follow MDN Document: execCommand() and practical beginner patterns. Always treat this API as legacy.
📚 Getting Started
Run a formatting command on a contenteditable region.
Example 1 — Toggle bold on a selection
Select text in the editable box, then run the bold command.
bold toggled
(selected text becomes bold / unbold)
How It Works
MDN: formatting commands like bold act on the current selection (or at the insertion point). Focus the editable element first so the selection is active.
Example 2 — MDN-style insertText
Insert plain text at the caret while trying to keep undo history.
bold: yes
insertText: yes
copy: yes
paste: no
(exact results vary by browser)
How It Works
Support is uneven — especially for clipboard commands. Combine this check with real user-gesture testing; MDN warns the execCommand return value alone is not a pre-flight probe.
Example 4 — Insert then undo
Show why undo history matters for some legacy inserts.
MDN highlights undo as a reason some authors still touch execCommand. Behavior still varies — always verify in your target browsers.
Example 5 — Prefer the Clipboard API for copy
MDN recommends this path instead of execCommand("copy").
JavaScript
async function copyModern(text) {
if (!navigator.clipboard || !navigator.clipboard.writeText) {
console.log("Clipboard API unavailable");
return;
}
await navigator.clipboard.writeText(text);
console.log("copied with Clipboard API");
}
copyModern("Hello from CodeToFun");
Document.execCommand() is Deprecated and Non-standard on MDN (not part of any current specification). Logos use the shared browser-image-sprite.png sprite from this project. Still present in many engines for legacy editing, but do not build new products on it.
✓ Deprecated · Non-standard
Document.execCommand()
Legacy editing / clipboard command switchboard — still seen in old editors; prefer Clipboard API and modern editor stacks.
LegacyNot for new apps
Google ChromeLegacy editing commands still present — avoid for new apps
Legacy
Mozilla FirefoxLegacy path; nested calls may fail — avoid for new apps
Legacy
Apple SafariLegacy editing support; verify each command
Legacy
Microsoft EdgeChromium legacy path — avoid for new code
Legacy
OperaFollow Chromium legacy behavior
Legacy
Internet ExplorerHistoric rich-text path only
Legacy
execCommand()Avoid
Bottom line: Learn it for legacy code and rare undo-preserving inserts. For clipboard and new rich-text UX, prefer modern APIs and editor libraries.
Wrap Up
Conclusion
document.execCommand(command, false, value) was the browser’s all-in-one remote for rich-text and clipboard actions. MDN marks it deprecated and non-standard. Understand it for legacy editors and occasional undo-preserving inserts, then move clipboard work to the Clipboard API and new editors to modern stacks.
Ignore permissions / secure-context rules for modern clipboard APIs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about execCommand()
Legacy editing remote — know it, rarely ship it.
5
Core concepts
📝01
Returns
boolean
MDN
⚠️02
Status
Deprecated
MDN
🛡03
Also
Non-standard
MDN
📋04
Niche
undo buffer
MDN note
📋05
Clipboard
Clipboard API
prefer
❓ Frequently Asked Questions
MDN: Document.execCommand() runs editing or clipboard-related commands — for example bold/italic on a selection, insertText into contenteditable or form fields, 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 no longer on track to become a standard.
MDN notes some cases still lack full alternatives — for example modifications via execCommand can preserve the undo buffer (edit history), unlike some direct DOM edits. If you use it, test cross-browser and consider queryCommandSupported().
MDN recommends the Clipboard API (for example navigator.clipboard.writeText) over execCommand copy/cut/paste for new work.
MDN: it returns a boolean that is false if the command is unsupported or disabled. Note: true only when invoked as part of a user interaction — you cannot use the return value alone to probe support before a user gesture.
MDN: beforeinput and input may or may not fire depending on the browser. Handlers can run before execCommand returns. Nested execCommand calls can fail (e.g. Firefox 82+).
Did you know?
The old useCSS command is logically backwards on MDN (false means use CSS, true means HTML tags — and it was replaced by styleWithCSS. Tiny quirks like that are why this API is a museum piece, not a foundation.