v135 · dom
Invoker Commands; the command and commandfor attributes
Adding command and commandfor attributes to <button> elements would allow authors to assign behaviour to buttons in a more accessible and declarative way, while reducing bugs and simplifying the amount of JavaScript pages are required to ship for interactivity. Buttons with commandfor and command attributes will - when clicked, touched, or enacted via keypre
concepts
-
command + commandfor, no JS
Pick a built-in command (
show-modal,toggle-popover,close) from the menu and watch a real button drive a<dialog>or[popover]target without a single event listener. -
Custom buttons open native pickers
The explainer's secondary use case:
command="show-picker"lets your own branded button trigger the native date / colour / file / month picker on any<input>. -
Custom command protocol
Beyond the built-ins, custom commands use a
--prefix. A mini audio-player remote dispatches--play,--pause,--seekviacommandfor, and the target listens via thecommandevent — no click handlers. -
Event delegation bench
Side-by-side: 16 buttons firing on a parent container under both the new
commandevent and the classicclick+closest()dance. See how delegation readsevent.commandandevent.sourcewith zero per-button bookkeeping. -
Settings panel
An app toolbar with four buttons — Account, Preferences, Share, and Help — each wired to a different
<dialog>or[popover]target viacommandforandcommand. Not a single click listener on any button. The close buttons inside each panel also usecommand="close".
why it shipped
All elements within the DOM are capable of having interactions added to them. A long while ago this took the form of adding inline JavaScript to an event attribute, such as <button onclick="other.open()"></button>. Inline JavaScript has (rightly so) fallen out of favour due to the security and maintainability concerns. Newer pages may instead introduce more JavaScript to imperatively discover elements and call addEventListener('click', ...) to invoke the same behaviour. These patterns reduce developer experience and introduce more boilerplate and friction, while remediating security and mainta