ElementInternals: form プロパティ

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.

formElementInternals インターフェイスの読み取り専用プロパティで、この要素に関連付けられた HTMLFormElement を返します。

HTMLFormElement です。

次の例では、 ID が myForm のフォーム内にカスタムチェックボックスコンポーネントを配置しています。 コンソールに form.length を出力すると、 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);

仕様書

Specification
HTML
# dom-elementinternals-form

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
form

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support