Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

command: give a button a declarative action

Invoke native dialog or popover actions, or dispatch a custom command.

Version: HTML Living Standard

Syntax

<button type="button" command="show-modal" commandfor="details">Open</button>

The command attribute gives a button an action for the element identified by its command target. Built-in values include show-modal, close and request-close for dialogs, plus popover show, hide and toggle actions. The action must be valid for the target. Missing or unrecognized values do not become arbitrary JavaScript or execute source text.

Custom command names start with -- and dispatch an event for application code to handle; they do not invent a native operation. A canceled command event prevents the default command action. Use type="button" when the control should not submit or reset a form. A disabled button does not invoke the action, and newer command support needs capability checks.

The example uses two native buttons to show a modal dialog and close it. Chromium 145 executes both actions without application handlers in the supported branch. A separate fallback installs explicit handlers when the DOM capability is absent. The tested result covers this browser, not every embedded WebView or older engine.

Examples

<!doctype html><meta charset="utf-8">
<button id="open" type="button" command="show-modal" commandfor="details">Open</button>
<dialog id="details"><p>Details</p><button id="close" type="button" command="close" commandfor="details">Close</button></dialog>
<output id="result"></output><script>
const open = document.querySelector('#open'), details = document.querySelector('#details');
if ('command' in open && 'commandForElement' in open) {
  open.click(); const opened = details.open && details.matches(':modal');
  document.querySelector('#close').click();
  document.querySelector('#result').textContent = 'opened=' + opened + '; closed=' + !details.open;
} else {
  open.onclick = () => details.showModal();
  document.querySelector('#close').onclick = () => details.close();
  document.querySelector('#result').textContent = 'command unsupported; explicit handlers installed';
}
</script>

Result

opened=true; closed=true

In the verified branch the dialog is opened and closed by native commands; fallback handlers are not installed.

Sources