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月.
form は ElementInternals インターフェイスの読み取り専用プロパティで、この要素に関連付けられた 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> |
ブラウザーの互換性
Loading…