W3cubDocs

/Web APIs

Invoker Commands API

The Invoker Commands API provides a way to declaratively assign behaviors to buttons, allowing control of interactive elements when the button is enacted (clicked or invoked via a keypress, such as the spacebar or return key).

Concepts and usage

A common pattern on the web is to have <button> elements control various aspects of the page, such as opening and closing popovers or <dialog> elements, formatting text, and more.

Historically creating these kinds of controls has required JavaScript event listeners added to the button which can then call the APIs on the element they control. The commandForElement and command properties provide a way to do this declaratively for a limited set of actions. This can be advantageous for built-in commands as the user does not have to wait for JavaScript to download and execute to make these buttons interactive.

HTML attributes

commandfor

Turns a <button> element into a button, controlling the given interactive element; takes the ID of the element to control as its value.

command

Specifies the action to be performed on an element being controlled by a control <button>, specified via the commandfor attribute.

Interfaces

CommandEvent

Represents an event notifying the user that a command has been issued. It is the event object for the command event. The event fires on element referenced by commandForElement.

Extensions to other interfaces

>

Instance properties

HTMLButtonElement.commandForElement

Gets and sets the element being controlled by the button. The JavaScript equivalent of the commandfor HTML attribute.

HTMLButtonElement.command

Gets and sets the action to be performed on the element being controlled by the button. Reflects the value of the command HTML attribute.

Events

command event

Fired on the element referenced by the button.

Examples

>

Creating declarative popovers

<button commandfor="mypopover" command="toggle-popover">
  Toggle the popover
</button>
<div id="mypopover" popover>
  <button commandfor="mypopover" command="hide-popover">Close</button>
  Popover content
</div>

Creating declarative dialogs

<button commandfor="mydialog" command="show-modal">Show modal dialog</button>
<dialog id="mydialog">
  <button commandfor="mydialog" command="close">Close</button>
  Dialog Content
</dialog>

Creating custom commands

<button commandfor="my-img" command="--rotate-left">Rotate left</button>
<button commandfor="my-img" command="--rotate-right">Rotate right</button>
<img id="my-img" src="photo.jpg" alt="[add appropriate alt text here]" />
const myImg = document.getElementById("my-img");

myImg.addEventListener("command", (event) => {
  if (event.command === "--rotate-left") {
    myImg.style.rotate = "-90deg";
  } else if (event.command === "--rotate-right") {
    myImg.style.rotate = "90deg";
  }
});

Specifications

Browser compatibility

>

api.CommandEvent

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
CommandEvent 135 135 preview 120 preview 135 No 89 No No 135 No
Invoker_Commands_API 135 135 preview 120 preview 135 No 89 No No 135 No
command 135 135 preview 120 preview 135 No 89 No No 135 No
source 135 135 preview 120 preview 135 No 89 No No 135 No

api.HTMLButtonElement.commandForElement

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
Invoker_Commands_API 135 135 preview 120 preview 135 No 89 No No 135 No

api.HTMLButtonElement.command

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
Invoker_Commands_API 135 135 preview 120 preview 135 No 89 No No 135 No
request-close 139 139 preview 123 preview 139 No 91 No No 139 No

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API