Invoker Commands

<button commandfor="id" command="..."> — trigger actions on target elements via HTML attributes. Custom commands prefixed with -- dispatch a CommandEvent.

1. Fullscreen — --toggle-fullscreen

Toggle fullscreen on any element using a custom invoker command. The button dispatches a CommandEvent — one event listener handles it.

Fullscreen Section

This section goes fullscreen via command="--toggle-fullscreen".

2. Counter — --increment / --decrement

Custom commands (prefixed with --) dispatch a CommandEvent on the target element. Only the event listener needs JS.

0
Implementation: Invoker Commands with custom commands
<!-- Fullscreen via custom command -->
<button commandfor="my-section" command="--toggle-fullscreen">
  Toggle Fullscreen
</button>
<section id="my-section">...</section>

<!-- Counter via custom commands -->
<button commandfor="counter" command="--increment">+</button>
<button commandfor="counter" command="--decrement">-</button>
<span id="counter">0</span>

<script>
  // Fullscreen handler — 5 lines
  const section = document.getElementById('my-section');
  section.addEventListener('command', (e) => {
    if (e.command === '--toggle-fullscreen') {
      if (document.fullscreenElement) document.exitFullscreen();
      else section.requestFullscreen();
    }
  });

  // Counter handler — 5 lines
  const counter = document.getElementById('counter');
  counter.addEventListener('command', (e) => {
    let n = parseInt(counter.textContent);
    if (e.command === '--increment') n++;
    if (e.command === '--decrement') n--;
    if (e.command === '--reset') n = 0;
    counter.textContent = n;
  });
</script>