ElementInternals: form-Eigenschaft
Baseline 2023Newly 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
Ein HTMLFormElement
.
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
BCD tables only load in the browser