JavaScript Document queryCommandEnabled() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Non-standard
Instance method

What You’ll Learn

document.queryCommandEnabled() is a deprecated, non-standard instance method that reports whether a named editor command is currently enabled (see MDN Document: queryCommandEnabled()). Learn the boolean return value, the MDN SelectAll pattern with execCommand(), cut/copy/paste caveats, how it differs from queryCommandSupported(), and five try-it labs.

01

Kind

Instance method

02

Args

command string

03

Returns

boolean

04

Pairs with

execCommand

05

Status

Deprecated

06

Also

Non-standard

Introduction

Legacy rich-text UIs often ask two questions before running a toolbar action: “Does this browser know the command?” and “Can I run it right now?” queryCommandEnabled() answers the second.

MDN: the method reports whether the specified editor command is enabled by the browser. A common pattern is to check SelectAll (or another command), then call document.execCommand(...) only when the check returns true.

💡
Think: “Is this command ready to run?”

1) Pick a command name (e.g. "SelectAll")
2) Call document.queryCommandEnabled(command)
3) If true, optionally run execCommand
4) Remember cut/copy need a user gesture (MDN)

Related tutorials: execCommand(), designMode, getSelection().

Understanding document.queryCommandEnabled()

An instance method on the page’s document object that probes whether a named editing/clipboard command is currently enabled (MDN).

  • command — string naming the command to check (MDN).
  • Return valuetrue if enabled, false otherwise (MDN).
  • Pairs with — deprecated document.execCommand() (MDN note).
  • cut / copy — MDN: only returns true from a user-initiated thread.
  • paste — MDN: false when unavailable or when privileges are insufficient.
  • vs supportedqueryCommandSupported = browser knows it; queryCommandEnabled = usable now.

📝 Syntax

General form of Document.queryCommandEnabled (MDN):

JavaScript
queryCommandEnabled(command)

Parameters

  • command — the command for which to determine support / enabled state (MDN). Examples: "SelectAll", "bold", "copy", "cut", "paste".

Return value

A boolean that is true if the command is enabled and false if it is not (MDN).

MDN Notes (clipboard commands)

  • For "cut" and "copy", the method only returns true when called from a user-initiated thread (MDN).
  • For "paste", false can mean unavailable or insufficient privileges (MDN).

MDN-style check before execCommand

JavaScript
const flg = document.queryCommandEnabled("SelectAll");

if (flg) {
  document.execCommand("SelectAll", false, null); // command is enabled, run it
}

⚡ Quick Reference

GoalCode
Check SelectAlldocument.queryCommandEnabled("SelectAll")
Run if enabledif (document.queryCommandEnabled("bold")) document.execCommand("bold", false, null)
Probe supportdocument.queryCommandSupported("bold")
Modern copyawait navigator.clipboard.writeText(text)
Feature-detect methodtypeof document.queryCommandEnabled === "function"
MDN statusDeprecated & Non-standard

🔍 At a Glance

Four facts about document.queryCommandEnabled().

Returns
boolean

enabled?

Arg
command

string

cut/copy
user gesture

MDN

Status
Deprecated

+ Non-standard

📋 Enabled vs Supported

queryCommandEnabledqueryCommandSupported
MeaningCan run now?Exists in this browser?
Clipboard nuancecut/copy need user thread (MDN)May report yes even when disabled
Typical useGate a toolbar button / execCommandFeature list / capability probe
StatusDeprecated & Non-standardSame legacy family

Examples Gallery

Examples follow MDN Document: queryCommandEnabled() and practical beginner patterns. Always treat this API as legacy.

📚 Getting Started

MDN’s core pattern: check, then run execCommand.

Example 1 — MDN: check SelectAll then run it

Only call execCommand when the command is enabled.

JavaScript
const flg = document.queryCommandEnabled("SelectAll");
console.log("SelectAll enabled:", flg);

if (flg) {
  document.execCommand("SelectAll", false, null);
  console.log("SelectAll ran");
}
Try It Yourself

How It Works

This is MDN’s documented example. See the full execCommand() tutorial for command lists and caveats.

Example 2 — Feature-detect the method itself

Guard against engines that remove legacy editing APIs.

JavaScript
const hasApi = typeof document.queryCommandEnabled === "function";
console.log("queryCommandEnabled available:", hasApi);

if (hasApi) {
  console.log("bold enabled:", document.queryCommandEnabled("bold"));
} else {
  console.log("Use a modern editor / Clipboard API instead");
}
Try It Yourself

How It Works

Deprecated APIs can disappear. Always feature-detect before calling, then prefer modern alternatives for new work.

📈 Practical Patterns

Toolbar gating, clipboard notes, and supported vs enabled.

Example 3 — Gate a legacy toolbar button

Disable UI when the command is not currently enabled.

JavaScript
const btn = document.getElementById("boldBtn");
const editor = document.getElementById("editor");
editor.focus();

const enabled =
  typeof document.queryCommandEnabled === "function" &&
  document.queryCommandEnabled("bold");

btn.disabled = !enabled;
btn.textContent = enabled ? "Bold (enabled)" : "Bold (disabled)";
console.log("toolbar bold enabled:", enabled);
Try It Yourself

How It Works

Focus a contenteditable region first so formatting commands have a valid editing context.

Example 4 — MDN notes for cut / copy / paste

Clipboard commands often report false outside a user gesture.

JavaScript
function reportClipboardCommands() {
  const names = ["cut", "copy", "paste"];
  return names.map((name) => {
    const enabled =
      typeof document.queryCommandEnabled === "function"
        ? document.queryCommandEnabled(name)
        : false;
    return name + ": " + enabled;
  }).join("\n");
}

// Cold call (no click) — cut/copy often false (MDN)
console.log(reportClipboardCommands());

// Prefer modern clipboard for new apps:
// await navigator.clipboard.writeText("Hello");
Try It Yourself

How It Works

MDN: cut/copy need a user-initiated thread; paste can also fail on privileges. For new clipboard UX, use the Clipboard API instead of execCommand("copy").

Example 5 — Enabled vs supported side by side

Compare both probes for the same command names.

JavaScript
const names = ["bold", "SelectAll", "copy", "paste"];
const lines = names.map((name) => {
  const supported =
    typeof document.queryCommandSupported === "function"
      ? document.queryCommandSupported(name)
      : false;
  const enabled =
    typeof document.queryCommandEnabled === "function"
      ? document.queryCommandEnabled(name)
      : false;
  return name + " → supported=" + supported + ", enabled=" + enabled;
});
console.log(lines.join("\n"));
Try It Yourself

How It Works

A command can be “supported” yet not “enabled” in the current context — especially clipboard commands outside a user gesture.

🚀 Common Use Cases

  • Legacy toolbar enable/disable — grey out Bold when the command is not enabled.
  • Safe execCommand gate — MDN: check before running a deprecated command.
  • Maintaining old editors — understand why cut/copy buttons stay disabled.
  • Interview / history knowledge — enabled vs supported in the execCommand family.
  • Not new clipboard UX — use the Clipboard API instead (MDN / execCommand guidance).
  • Not a modern editor foundation — prefer dedicated libraries for new products.

🧠 How queryCommandEnabled() Works

1

Choose a command name

Pass a string such as "SelectAll", "bold", or "copy" (MDN).

command
2

Browser evaluates context

Focus, selection, privileges, and user-gesture rules affect the result (MDN notes for clipboard).

Context
3

Return a boolean

true means enabled; false means not enabled (MDN).

boolean
4

Optionally call execCommand

MDN suggests this check when you still use deprecated execCommand. Prefer modern APIs when possible.

📝 Notes

  • MDN: Deprecated and Non-standard; not part of any current specification.
  • MDN: if you still use execCommand(), consider queryCommandEnabled() for compatibility checks.
  • MDN: cut/copy only return true from a user-initiated thread.
  • MDN: paste can return false for privilege reasons, not only missing support.
  • Do not confuse “enabled” with “supported” — check both in legacy UIs.
  • Related: execCommand(), designMode, getSelection().

Legacy Browser Support

Document.queryCommandEnabled() 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.queryCommandEnabled()

Legacy enabled-check for editor commands — still seen with old toolbars; prefer modern editors and the Clipboard API.

Legacy Not for new apps
Google Chrome Legacy editing probe still present — avoid for new apps
Legacy
Mozilla Firefox Legacy path; cut/copy need user gesture — avoid for new apps
Legacy
Apple Safari Legacy editing support; verify each command
Legacy
Microsoft Edge Chromium legacy path — avoid for new code
Legacy
Opera Follow Chromium legacy behavior
Legacy
Internet Explorer Historic rich-text path only
Legacy
queryCommandEnabled() Avoid

Bottom line: Learn it for legacy execCommand toolbars and interviews. For new clipboard and rich-text UX, prefer modern APIs and editor libraries.

Conclusion

document.queryCommandEnabled(command) returns a boolean telling you whether a legacy editor command is currently enabled. MDN marks it deprecated and non-standard. Use it only when maintaining execCommand-based UIs; for new apps, choose modern editors and the Clipboard API.

Continue with queryCommandState(), exitFullscreen(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect typeof document.queryCommandEnabled === "function"
  • Check enabled state before calling legacy execCommand (MDN)
  • Call cut/copy probes from a real user gesture when testing (MDN)
  • Prefer the Clipboard API for new copy / paste UX
  • Compare with queryCommandSupported when debugging toolbars

❌ Don’t

  • Build a new rich-text product on queryCommand* + execCommand
  • Assume true for cut/copy outside a user-initiated thread (MDN)
  • Treat paste: false as “unsupported only” (privileges matter)
  • Confuse enabled with supported
  • Ignore that the whole editing command family is non-standard

Key Takeaways

Knowledge Unlocked

Five things to remember about queryCommandEnabled()

Legacy enabled probe for editor commands — know it, rarely ship it.

5
Core concepts
⚠️02

Status

Deprecated

MDN
🛡03

Also

Non-standard

MDN
📋04

Pairs with

execCommand

legacy
📋05

cut/copy

user gesture

MDN note

❓ Frequently Asked Questions

MDN: Document.queryCommandEnabled() reports whether the specified editor command is enabled by the browser. It returns true if the command is enabled and false if it is not.
Yes. MDN marks Document.queryCommandEnabled() as Deprecated and Non-standard. It is not part of any current specification and is no longer on track to become a standard.
MDN: if you still use deprecated execCommand() for a rare legacy reason, consider checking the command with queryCommandEnabled() first for compatibility. Prefer modern APIs for new products.
queryCommandSupported() asks whether the browser supports a command at all. queryCommandEnabled() asks whether that command is currently enabled (for example, cut/copy often need a user-initiated thread).
MDN: for cut and copy, the method only returns true when called from a user-initiated thread. Outside a click or similar gesture, expect false even if the command exists.
MDN: paste returns false not only when unavailable, but also when the calling script lacks privileges to perform the action.
Did you know?

MDN explicitly pairs this method with deprecated execCommand(): if you still call execCommand for a rare legacy reason, check queryCommandEnabled() first. That pairing is why this tutorial sits next to the execCommand page in the Document methods list.

Next: queryCommandState()

Learn how to check whether a legacy editor command is already applied to the current selection.

queryCommandState() →

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.

6 people found this page helpful