ElementInternals: form property

Baseline 2023
Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Examples

The following example shows a custom checkbox component inside a form with an ID of myForm. Printing form.length to the console, gives us the value of HTMLFormElement.length.

html
<form id="myForm">
  <custom-checkbox id="custom-checkbox"></custom-checkbox>
  <custom-label for="custom-checkbox">Join newsletter</custom-label>
</form>
js
class CustomCheckbox extends HTMLElement {
  static formAssociated = true;
  #internals;

  constructor() {
    super();
    this.#internals = this.attachInternals();
  }

  connectedCallback() {
    console.log(this.#internals.form.length);
  }
}

window.customElements.define("custom-checkbox", CustomCheckbox);

Specifications

Specification
Product help
  • ariaColCount
  • ariaCurrent
  • ariaHasPopup
  • ariaLevel
  • ariaMultiSelectable
  • ariaPressed
  • ariaRoleDescription
  • ariaRowSpan
  • ariaValueMax
  • form
  • states
  • checkValidity()
  • CustomElementRegistry
  • Element.shadowRoot
  • HTMLElement.attachInternals()
  • Node.isConnected
  • Window.customElements
  • ","source":{"folder":"en-us/web/api/elementinternals/form","github_url":"https://github.com/mdn/content/blob/main/files/en-us/web/api/elementinternals/form/index.md","last_commit_url":"https://github.com/mdn/content/commit/ce10da0e9d23d241b175d8d68bf93507734b7c48","filename":"index.md"},"summary":"The form read-only property of the ElementInternals interface returns the HTMLFormElement associated with this element.","title":"ElementInternals: form property","toc":[{"text":"Value","id":"value"},{"text":"Examples","id":"examples"},{"text":"Specifications","id":"specifications"},{"text":"Browser compatibility","id":"browser_compatibility"}],"baseline":{"baseline":"low","baseline_low_date":"2023-03-27","support":{"chrome":"77","chrome_android":"77","edge":"79","firefox":"98","firefox_android":"98","safari":"16.4","safari_ios":"16.4"}},"browserCompat":["api.ElementInternals.form"],"pageType":"web-api-instance-property"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant