DOM Methods
ElementInternals.setFormValue()
Sets the custom control’s contribution to form data. A string or File supplies one value, FormData can supply multiple named entries, and null omits submission. Optional state can preserve restoration information distinct from the submitted value. Example result: Baku.
Formal Syntax & Structure
html syntaxhtml
internals.setFormValue(value, state?)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| value | string | File | FormData | null | Submission value. |
| state | string | File | FormData | null | Optional restoration state. |
Return Value
undefined
No return value.
Working Example
Example Snippethtml
<form id="demo-form"> <demo-field name="city"></demo-field></form><p> Result: <output id="result"></output></p><script> const result = document.getElementById("result"); (async () => { class DemoField extends HTMLElement { static formAssociated = true; constructor() { super(); this.internals = this.attachInternals(); } } customElements.define("demo-field", DemoField); const field = document.querySelector("demo-field"); const internals = field.internals; internals.setFormValue("Baku"); result.textContent = new FormData(internals.form).get("city"); })().catch(error => { result.textContent = "Error: " + error.name; });</script>