# \[command\]

> The `command` HTML attribute specifies the action a button performs on the element referenced by `commandfor`.

## How do I open a dialog with the HTML command attribute?

The attribute of the `<button>` element that defines the action performed on the target element. Built-in values include 'show-modal', 'close', 'request-close', 'toggle-popover', 'show-popover' and 'hide-popover'. Custom commands start with '--' and dispatch a `command` event.

- Topic: Element Attributes
- Type: attribute
- Browser support: Chrome 135+, Chrome Android 135+, Edge 135+, Firefox 144+, Firefox Android 144+, Safari 26.2+, Safari iOS 26.2+

## Example

Wrong:

```html
<button onclick="dlg.showModal()">Open</button>
<dialog id="dlg">...</dialog>
```

Right:

```html
<button commandfor="dlg" command="show-modal">Open</button>
<dialog id="dlg">...</dialog>
```

The button opens the modal dialog declaratively, with no click handler.

## Common mistakes

- Writing a custom command without the leading '--', so the browser ignores it.

## Key points

- A button that opens a dialog uses `command='show-modal'` with `commandfor`, not an `onclick` handler calling `showModal()`.
- Custom commands start with two dashes, like '--rotate', and are handled in a `command` event listener on the target.
- The button has visible text or an `aria-label` that names the action.

## Interview questions

### How do I close a dialog with the command attribute?

Add a button inside it with `commandfor` set to the dialog id and `command='close'`.

### How do I handle a custom command?

Listen for the `command` event on the target and read `event.command`, like '--rotate'.

## Related cards

- [\[commandfor\]](https://wonderium.dev/html/commandfor.md)
- [`<dialog>`](https://wonderium.dev/html/dialog.md)
- [\[popover\]](https://wonderium.dev/html/popover.md)
- [`<button>`](https://wonderium.dev/html/button.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/command-attribute
- Last updated: 2026-11-25
