ElementInternals: form-Eigenschaft

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.

Die schreibgeschützte form-Eigenschaft des ElementInternals-Interfaces gibt das HTMLFormElement zurück, das mit diesem Element verknüpft ist.

Wert

Beispiele

Das folgende Beispiel zeigt eine benutzerdefinierte Checkbox-Komponente innerhalb eines Formulars mit der ID myForm. Das Ausdrucken von form.length in die Konsole gibt uns den Wert von HTMLFormElement.length zurück.

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);

Spezifikationen

Specification
HTML
# dom-elementinternals-form

Browser-Kompatibilität

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