HTML Form-Associated Custom Elements: ElementInternals
Associate a custom element with a form
An autonomous custom element can opt in to native form association using static formAssociated = true. attachInternals() returns an ElementInternals object; setFormValue sets the submission value. This minimal example models a fixed value, not an interactive input. The host name supplies the entry key.
<form id="form"> <study-value name="topic"></study-value></form><output id="result"></output><script> class StudyValue extends HTMLElement { static formAssociated = true; constructor() { super(); this.internals = this.attachInternals(); this.internals.setFormValue("HTML"); } } customElements.define("study-value", StudyValue); const form = document.querySelector("#form"); document.querySelector("#result").value = new FormData(form).get("topic");</script>The form entry topic has value HTML. No request is sent.
Synchronize a native input and validity
The component can keep a native input in its shadow root for keyboard interaction. On input, synchronize the submission value and setValidity. An empty object clears validity errors; a true flag requires an error message. The internal input has a visible label, and the input is passed as the validation anchor. The host is initially invalid.
<form id="form"> <study-field name="topic"></study-field></form><script> class StudyField extends HTMLElement { static formAssociated = true; constructor() { super(); this.internals = this.attachInternals(); const root = this.attachShadow({mode: "open"}); root.innerHTML = ``; this.input = root.querySelector("input"); this.input.addEventListener("input", () => this.sync()); this.sync(); } sync() { const value = this.input.value; this.internals.setFormValue(value); this.internals.setValidity(value ? {} : {valueMissing: true}, value ? "" : "Enter a topic", this.input); } formDisabledCallback(disabled) { this.input.disabled = disabled; } formResetCallback() { this.input.value = ""; this.sync(); } } customElements.define("study-field", StudyField);</script>The empty field causes form.checkValidity() to return false.
Implement reset and disabled callbacks
Form-associated elements have specialized callbacks. formResetCallback restores the default state; formDisabledCallback receives effective disabled state, including ancestor fieldsets. A disabled host is excluded from form data. setFormValue(null) can omit an entry even when enabled. Here the component starts with a default, changes value, then resets.
<form id="form"> <study-choice name="topic"></study-choice></form><output id="result"></output><script> class StudyChoice extends HTMLElement { static formAssociated = true; constructor() { super(); this.internals = this.attachInternals(); this.setValue("HTML"); } setValue(value) { this.internals.setFormValue(value); } formResetCallback() { this.setValue("HTML"); } } customElements.define("study-choice", StudyChoice); const form = document.querySelector("#form"); form.querySelector("study-choice").setValue("CSS"); form.reset(); document.querySelector("#result").value = new FormData(form).get("topic");</script>After reset, the submitted value is HTML again.
Common mistakes and practice
Prefer native controls when they meet the requirements. A production custom control also needs robust labeling, focus, state restoration, disabled behavior, accessibility testing and a fallback for unsupported browsers. Client validity does not replace server checks. The fixed-value examples are deliberately not full input widgets.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Form-Associated Custom Elements: ElementInternals — Knowledge Check
Apply the components concepts taught in this lesson.
What opts an autonomous custom element into form association?
Which method supplies a form submission value?
How can all custom validity flags be cleared?
Which callback handles form reset?
Does client validity replace server validation?