Newly availableUseful in modern browsers, but confirm support before making it a core requirement.

Overview

The command and commandfor attributes for the <button> HTML element dispatch an action to an element when the button is invoked (by click or keystroke), as a declarative alternative to addEventListener() calls or onclick attribute handlers. It is most useful when native HTML semantics or browser capabilities can replace custom implementation work.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
135
135
144
26.2
135
26.2
HTML attribute
command
135
135
144
26.2
135
26.2
commandfor
135
135
144
26.2
135
26.2
DOM API

The command read-only property of the CommandEvent interface returns a string containing the value of the HTMLButtonElement.command property at the time the event was dispatched.

135
135
144
26.2
135
26.2

The CommandEvent() constructor creates a new CommandEvent object.

135
135
144
26.2
135
26.2

The source read-only property of the CommandEvent interface returns an EventTarget representing the control that invoked the given command.

135
135
144
26.2
135
26.2

The command property of the HTMLButtonElement interface gets and sets the action to be performed on an element being controlled by this button. For this to have an effect, commandfor must be set.

135
135
144
26.2
135
26.2

The commandForElement property of the HTMLButtonElement interface gets and sets the element to control via a button.

135
135
144
26.2
135
26.2

The command event of the HTMLElement interface fires on an element that is controlled via a HTMLButtonElement with valid HTMLButtonElement.commandForElement and HTMLButtonElement.command values, whenever the button is interacted with (e.g., it is clicked).

135
135
144
26.2
135
26.2
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

HTML
<button commandfor="my-dialog" command="show-modal">
  Open the dialog
</button>
<dialog id="my-dialog">
  <p>Content</p>
  <button commandfor="my-dialog" command="close">Close</button>
</dialog>

Live demo

Command-driven popover

Use command and commandfor to toggle a related popover without custom script.

PreviewFullscreen

Command examples

Show the built-in actions commonly paired with commandfor.

PreviewFullscreen

Why it matters

Explain the value of declarative commands for simple UI relationships.

PreviewFullscreen

Use cases

  • Improve form input

    Use Invoker commands to make data entry more efficient, guided, or predictable inside real forms.

  • Reduce friction

    Apply Invoker commands when the browser can help users complete a task with fewer mistakes or less repetitive work.

Cautions

  • Test Invoker commands in your target browsers and input environments before depending on it as a primary behavior.
  • Provide a fallback path or acceptable degradation strategy when support is still limited.

Accessibility

  • Keep labels, instructions, validation, and keyboard behavior clear so the enhancement helps rather than surprises users.

Powered by web-features