W3cubDocs

/Web APIs

HTMLElement: command event

Limited availability

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

The command event of the HTMLElement interface fires on an element that is controlled via a button with valid commandForElement and command values, whenever the button is interacted with (e.g., it is clicked).

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

addEventListener("command", (event) => { })

oncommand = (event) => { }

Event type

A CommandEvent. Inherits from Event.

Event CommandEvent

Examples

>

Basic example

const popover = document.getElementById("mypopover");

// …

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

Event dispatch and cancellation

It is worth pointing out that command events fire on the element being invoked. If the button is clicked, it will first dispatch a click event which, if cancelled, then the command event will not fire and the default behavior will not be run. In addition to cancelling the click event on the button, it is also possible to cancel the command event.

For example:

button.addEventListener("click", (event) => {
  event.preventDefault(); // the `command` event will never fire
});
element.addEventListener("command", (event) => {
  event.preventDefault(); // the `command` event fires but the default behavior is cancelled
});

Specifications

Specification
HTML>
# event-command>

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
command_event 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/HTMLElement/command_event