ElementInternals exposes browser capabilities that let a custom element participate in form submission. A component opts into form association with a static declaration, obtains its internals with attachInternals(), and publishes values through setFormValue(). section 3.9
A form-associated temperature gauge
A vanilla custom element can submit its current value like this: static formAssociated = true; #internals = this.attachInternals(); connectedCallback() { this.#internals.setFormValue(this.value); } With <temperature-gauge name="temperature"> inside a form, the submitted key is temperature. Call setFormValue() again whenever the component value changes. section 3.9
Use ElementInternals for these form behaviors:
- Submit multiple values by constructing FormData, setting entries such as color and size, and passing it to setFormValue(). section 3.9
- Support reset behavior by saving initial submit-able properties in formAssociatedCallback() and restoring them in formResetCallback(). section 3.9
- In Stencil, enable form association in the component configuration, inject internals with @AttachInternals(), and update the value during componentWillLoad() and on changes. section 6.12
const formData = new FormData();
formData.set("color", someColor);
formData.set("size", someSize);
this.#internals.setFormValue(formData);
Inspect the FormData pattern for submitting several values from one custom element. section 3.9

