Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLElement
  4. HTMLElement: command event

HTMLElement: command event

Limited availability

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

Thecommand event of theHTMLElement interface fires on an element that is controlled via abutton with validcommandForElement andcommand values, whenever the button is interacted with (e.g., it is clicked).

Syntax

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

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

Event type

ACommandEvent. Inherits fromEvent.

Event CommandEvent

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 thatcommand events fire on the element being invoked. If the button is clicked, it will first dispatch aclick event which, if cancelled, then thecommand event will not fire and the default behavior will not be run.In addition to cancelling theclick event on the button, it is also possible to cancel thecommand 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

Specification
HTML
# event-command

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp