Opening and Closing Dialogs Without JavaScript Using HTML Invoker Commands

Learn how to use HTML invoker commands to open and close dialogs without writing any JavaScript.

The native <dialog> element was a huge step forward for web developers. It gave us a standardized way to create modal dialogs with built-in backdrop handling, focus management, and keyboard interactions—no more reinventing the wheel with div containers and mountains of JavaScript.

But we still needed JavaScript for one fundamental task: opening the dialog.

The HTML Invoker Commands API changes that. Now you can create fully functional dialogs with just HTML.

The Traditional Approach

Here’s what we typically had to write:

<button type="button" id="openBtn">Open Dialog</button>
<dialog id="dialog">
  <form method="dialog">
    <button type="submit">Close Dialog</button>
  </form>
  <!-- dialog content -->
</dialog>

<script>
  const dialog = document.getElementById('dialog');
  const openBtn = document.getElementById('openBtn');
  
  openBtn.addEventListener('click', () => {
    dialog.showModal();
  });
</script>

You could close the dialog without JavaScript using <form method="dialog">, but you still needed JavaScript to open it.

The Invoker Commands Approach

With Invoker Commands, you add two attributes to your button:

<button commandFor="dialog" command="show-modal" type="button">
  Open Dialog
</button>

<dialog id="dialog">
  <button commandFor="dialog" command="close" type="button">
    Close Dialog
  </button>
  <!-- dialog content -->
</dialog>

That’s it. No JavaScript required.

How It Works

The two key attributes are:

For dialogs, you have two command options:

What You Get for Free

When you use command="show-modal", the browser handles:

Browser Support

Invoker Commands are now supported across all modern browsers. This is still a rather new addition to the platform so, you will have to determine whether it is safe to use in your production environment. With that said, there is also a polyfill available by Keith Cirkel.

NOTE: The Invoker Commands API is supported by all modern browsers. See the browser compatibility table on MDN Web Docs for details.

Why This Matters

Invoker Commands represent a broader shift: giving HTML the power to handle common interactive patterns without requiring JavaScript. This means:

The web platform keeps getting better at being the web platform.