CKTIS / HTML
oncommand — handling the command event
The oncommand attribute specifies JavaScript to execute when a command event is received. It belongs on the element receiving the command; it neither selects the target nor initiates a command by itself.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
oncommand="JavaScript statements"oncommand is a global event handler content attribute for the command event, available on HTML elements. Its value is a JavaScript function body, not a command name. Its DOM counterpart, element.oncommand, can be assigned a function.
In the command mechanism, a button uses commandfor to identify the target element and command to specify the action. Place the oncommand handler on the target, not necessarily on the button. Custom command names start with --; author code implements their behavior.
Inside the handler, event.command contains the command, and event.source identifies the initiating element, subject to Shadow DOM retargeting rules. this refers to the element handling the event. In the example, the button sends --report, and the paragraph replaces its text with that value.
The example requires browser support for the relevant features and permission to run inline scripts. Content Security Policy can block the handler attribute; an application can instead register a handler using addEventListener('command', handler) in a permitted script.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>oncommand example</title>
<button type="button" commandfor="target" command="--report">Run</button>
<p id="target" oncommand="if (event.command === '--report') this.textContent = event.command;">Ready</p>
</html>Result
--report
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.