W3cubDocs

/Web APIs

CommandEvent

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The CommandEvent interface represents an event notifying the user when a button element with valid commandForElement and command attributes is about to invoke an interactive element.

This is the event object for the HTMLElement command event, which represents an action from an Invoker Control when it is invoked (for example when it is clicked or pressed).

Event CommandEvent

Constructor

CommandEvent()

Creates an CommandEvent object.

Instance properties

This interface inherits properties from its parent, Event.

CommandEvent.source Read only

An HTMLButtonElement representing the button that caused this invocation.

CommandEvent.command Read only

A string representing the command value of the source button.

Examples

>

Basic example

<button commandfor="mypopover" command="show-popover">Show popover</button>

<div popover id="mypopover" role="[declare appropriate ARIA role]">
  <!-- popover content here -->
  <button commandfor="mypopover" command="hide-popover">Hide popover</button>
</div>
const popover = document.getElementById("mypopover");

// …

popover.addEventListener("command", (event) => {
  if (event.command === "show-popover") {
    console.log("Popover is about to be shown");
  }
});

Custom example

<button commandfor="the-image" command="--rotate-left">Rotate Left</button>

<button commandfor="the-image" command="--rotate-right">Rotate Right</button>

<img id="the-image" src="photo.jpg" alt="[add appropriate alt text here]" />
const image = document.getElementById("the-image");

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

Specifications

Specification
HTML>
# commandevent>

Browser compatibility

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
CommandEvent 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

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/CommandEvent