このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

ElementInternals: form プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年3月.

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

ブラウザーの互換性