HTML Beginner

Modal controls without onclick handlers

Opening a dialog modally required onclick handlers calling showModal(). Invoker Commands let buttons perform actions on other elements declaratively with commandfor and command attributes.

Modern
6 lines
1<button commandfor="dlg"
2  command="show-modal">
3  Open Dialog
4</button>
5
6<dialog id="dlg">...</dialog>
Old 8 lines
1<!-- Inline onclick or JS event listener -->
2<button onclick="
3  document.querySelector('#dlg')
4  .showModal()">
5  Open Dialog
6</button>
7
8<dialog id="dlg">...</dialog>
Limited availability 72% global usage

This feature is not Baseline because it does not work in some of the most widely-used browsers.

135+
135+
commandfor + command β€” no onclick handler
Invoker Commands

Opened with commandfor + command="show-modal". No onclick anywhere.

commandfor + command β€” Chrome 135+
Not supported in this browser
commandfor + command require Chrome 135+
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

Declarative

Link a button to its target with commandfor (like the for attribute on labels). No querySelector, no addEventListener.

✦

Multiple commands

show-modal, close, show-popover, hide-popover, toggle-popover. One pattern for all interactive elements.

∞

Custom commands too

Prefix with -- for custom commands handled by the command event. Extensible without frameworks.

Lines Saved
8 β†’ 6
Zero JavaScript
Old Approach
onclick handlers
Inline JS or addEventListener
Modern Approach
Invoker Commands
commandfor + command

How it works

To open a <dialog> modally, you needed JavaScript: either an inline onclick that calls showModal(), or an addEventListener in a script. Same for popovers and closing. Every interactive trigger required its own JS wiring.

Invoker Commands (Chrome 135+) add two HTML attributes: commandfor takes the ID of the target element, and command specifies the action. Built-in commands mirror their JS counterparts: show-modal, close, show-popover, hide-popover, toggle-popover. Custom commands prefixed with -- fire a command event on the target for extensibility.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC