References

Beginner-friendly references for web development, with live, editable examples.

The HTML command attribute

Attribute Updated
Quick answer

The HTML command attribute declares a built-in command the button runs on the element named by commandfor (no JavaScript). It is used on the <button> element.

Overview

The command attribute declares a built-in command the button runs on the element named by commandfor (no JavaScript). It applies to the <button> element.

Part of the 2025 invoker commands API: a button can open a <dialog> or control a popover declaratively, paired with commandfor. Support is still rolling out.

Syntax

<button command="show-modal" commandfor="dlg">Open</button>
<dialog id="dlg"> … </dialog>

Values

Value
show-modal | close | request-close | toggle-popover | show-popover | hide-popover | a custom --command

Best practices

  • Always set an explicit type on a <button> — the default inside a form is submit.
  • Use the form* override attributes only when one button genuinely needs different submission behavior.
  • Give every button clear, descriptive text for screen readers.
  • Prefer real <button> elements over clickable <div>s for keyboard and accessibility support.

Frequently asked questions

What does the command attribute do?
Declares a built-in button command (invoker commands).
Why does my button submit the form unexpectedly?
A <button> inside a form defaults to type="submit". Add type="button" if it should not submit.
Can one button submit to a different URL than the form?
Yes. Put formaction (and optionally formmethod) on that submit button to override the form.
Which elements use the command attribute?
It is an element-specific attribute, used on <button> and submit-type <input> controls.