v149 · Custom Elements · ElementInternals

Custom Toggle Button

HTMLSubmitButtonBehavior in ElementInternals.behaviors lets a custom element participate in form submission just like a real <button type=submit>. No subclassing, no click handler boilerplate — the platform wires the semantics.

live demo

Submit →

before vs. after

BEFORE extending HTMLButtonElement
// Requires extending a built-in —
// not supported in Safari, needs polyfill
class MyBtn extends HTMLButtonElement {
  connectedCallback() {
    this.type = 'submit';
  }
}
customElements.define(
  'my-btn',
  MyBtn,
  { extends: 'button' } // ← the problem
);
AFTER HTMLSubmitButtonBehavior
class MyBtn extends HTMLElement {
  #internals;
  connectedCallback() {
    this.#internals =
      this.attachInternals();
    this.#internals.behaviors.add(
      HTMLSubmitButtonBehavior
    );
  }
}
customElements.define('my-submit-btn', MyBtn);

what HTMLSubmitButtonBehavior provides

see also

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗