References

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

The HTML commandfor attribute

Attribute Updated
Quick answer

The HTML commandfor attribute names the element (by id) that the button's command acts on. It is used on the <button> element.

Overview

The commandfor attribute targets the element a button command controls. It is used on <button> and submit-type <input> controls.

It applies to buttons. Several of these let a single submit button override the parent <form>'s settings (its action, method, encoding or validation), while others wire a button to a popover or a built-in command.

Syntax

<button command="toggle-popover" commandfor="p">Toggle</button>

Values

Value
The id of the target element.

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 commandfor attribute do?
Targets the element a button command controls.
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 commandfor attribute?
It is an element-specific attribute, used on <button> and submit-type <input> controls.