Favorites

HTML oncommand Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
oncommand
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 22nd November 2026

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

<!DOCTYPE html>
<html>
<head>
<title>oncommand event</title>
<style>
#box {
width: 100px;
height: 100px;
margin-top: 10px;
background: #4361ee;
color: #fff;
}
</style>
</head>
<body>
<button commandfor="box" command="--grow">Grow</button>
<button commandfor="box" command="--shrink">Shrink</button>
<div id="box" oncommand="handle(event)">Box</div>
<script>
var size = 100;
function handle(event) {
if (event.command === "--grow") size += 20;
if (event.command === "--shrink") size = Math.max(40, size - 20);
event.target.style.width = event.target.style.height = size + "px";
event.target.textContent = event.command + " from " + event.source.textContent;
}
</script>
</body>
</html>
Open in editor

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.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee