ElementInternals: form-Eigenschaft
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since März 2023.
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
Loading…