HTMLElement: command event

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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.

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

oncommand = (event) => {};

Event type

A CommandEvent. Inherits from Event.

Examples

Basic example

js
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:

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

Specifications

No specification found

No specification data found for api.HTMLElement.command_event.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

Browser compatibility

BCD tables only load in the browser

See also