HTML oncommand Event Attribute
Description
The oncommand event attribute runs a script when an element is controlled by an invoker button, that is, a <button> whose commandfor attribute points to the element's id. The event fires on the target element, not on the button.
The event is a CommandEvent with two useful properties: event.command, the value of the button's command attribute, and event.source, the button that was clicked. Built-in commands such as show-modal, close and toggle-popover are carried out by the browser unless the handler calls event.preventDefault(). Custom commands must start with two dashes, like --rotate, and do nothing unless a command event handler acts on them.
<button commandfor="img" command="--rotate">Rotate</button>
<img id="img" src="photo.jpg" alt="" oncommand="if (event.command === '--rotate') this.style.rotate = '90deg'">
Syntax
<element oncommand="script">
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML oncommand event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
Desktop





Tablets & Mobile





