Favorites

HTML commandfor Attribute

commandfor
HTML AttributeThis attribute is supported by all modern browsers.
Updated 22nd November 2026

The commandfor attribute names, by id, the element that a button controls with its command attribute, such as a dialog or popover.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Description

The commandfor attribute of the <button> element holds the id of the element that the button controls when it is activated, using the action named in its command attribute.

Together, command and commandfor form invoker commands: a declarative way for a button to control another element without JavaScript event handlers. commandfor holds the id of the element to control, and command names the action to perform on it.

Built-in commands include show-modal, close and request-close for <dialog> elements, and show-popover, hide-popover and toggle-popover for popovers. Custom commands start with two dashes (for example --rotate) and fire a command event on the target, which a script can handle.

Syntax

<button commandfor="id" command="value">

Values

  • idThe id of the element to control, in the same document (or shadow root) as the button.

Applies To

Example

<button commandfor="dlg" command="show-modal">Open dialog</button>
<dialog id="dlg">
<p>Opened without JavaScript.</p>
<button commandfor="dlg" command="close">Close</button>
</dialog>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML commandfor attribute. Hover over a browser icon to see the version that first introduced support for this HTML attribute.

This attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee