Events

oncommand

A command button targets an element with commandfor and dispatches a command to it. HTML event-handler content attribute or corresponding event-handler property; the event fires only on relevant targets. The example uses document.getElementById("demo"). Event interface: CommandEvent. For the described native event: bubbles: no; cancelable: yes. Read command and source; custom commands start with two hyphens. Newer support varies. Example: Click Refresh in a browser supporting invoker commands. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
<div oncommand="handleEvent(event)">  target.addEventListener("command", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
oncommandJavaScript handler bodyOptional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute.
eventCommandEventEvent object delivered to the addEventListener callback; event.type is "command". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<button commandfor="demo" command="--refresh" type="button">Refresh</button><div id="demo">Preview</div><p role="status" id="log">Waiting for command</p><script>  const target = document.getElementById("demo");  const log = document.getElementById("log");  target.addEventListener("command", event => {    log.textContent = String(event.command);  });</script>
HTML Reference