Content · Web Development · HTML

HTML Declarative Command Buttons: command and commandfor

Declarative Command Buttons: command and commandfor: The result depends on browser support and, in examples with script elements, on JavaScript being allowed. Includes a practical HTML example and an explanation.

1 views Published Oct 19, 2026 Updated Oct 19, 2026 Admin

Small interactions can make a webpage easier to use: opening a tip, changing a setting, or reusing an interface component.

What You Need to Know


Command and commandfor let a button request certain native actions, such as opening a dialog, in browsers that support them. Provide a tested fallback for users on older browsers.

In this example: open a native dialog with a command button.

Try the Example


Take a look at the example. We’ll explain the important lines right below it.

<button type="button" commandfor="help-dialog" command="show-modal">Open help</button>
<dialog id="help-dialog">
    <h2>Help</h2>
    <p>Use the Close button to return to the page.</p>
    <button type="button" commandfor="help-dialog" command="close">Close</button>
</dialog>

Let’s Read the Code


  • command: Command tells a declarative button which supported action to request.
  • <button>: The button offers an action; its type or command determines what happens.
  • <dialog>: Dialog provides a browser-managed dialog container; modal display may need showModal or a supported command.
  • type: Type changes what sort of control or resource the browser uses.

Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.

What Happens in the Browser?


When this example includes JavaScript, it supplies behavior that ordinary HTML does not automatically provide. Always think about a fallback.

Start with native behavior. Details, dialog, and popover each solve a different interface problem. Choose the simplest control that actually matches what a visitor wants to do, then test focus and dismissal as well as appearance.

Your Turn


Run this on a local development server if it loads modules or resources. Change the visible text or trigger and check what still works with scripts disabled.

Common Beginner Mistakes


  • Assuming new HTML syntax works in every browser without testing.
  • HTML supplies structure; JavaScript is necessary for the scripted behavior in this example. Check keyboard access and focus movement too.

Key Takeaways


  • Command and commandfor let a button request certain native actions, such as opening a dialog, in browsers that support them.
  • command: Command tells a declarative button which supported action to request.
  • Test the behavior you expect rather than trusting how the snippet looks in your editor.
Share this resourceSend this page to someone who may find it useful.