- From: Paul Kiddle <notifications@github.com>
- Date: Mon, 01 Sep 2025 07:16:25 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/1402@github.com>
PaulKiddle created an issue (whatwg/dom#1402) ### What problem are you trying to solve? Given a url-encoded string with multiple values, it would be useful to to be able to populate multiple form fields with that data using a single DOM method. Some use cases: - Store serialized form state and restore it at a later point in time - Populate form data from the page's query string on `popstate` events - Fill a form [using preset data](https://github.com/openui/open-ui/issues/1038#issuecomment-3236216057) via command buttons ### What solutions exist today? There is no direct way to do this currently, developers must manually iterate through the elements in a form or fieldset and set their values based on some input. The exact steps vary depending on the types of inputs in the form, so the code needed is not trivial to write. ### How would you solve it? Add a new method to `HTMLFormElement.prototype` and `HTMLFieldSet.prototype` that accepts a string or `URLSearchParams` object and does the following: - Let `elements` be a list of the form or fieldset's descendent `select` elements, `textarea` elements, `RadioNodeList`s, and `input`s whose type is **not** one of `button`, `file`, `hidden`, `reset`, `submit`, or `radio`, ordered as they appear in the document - Let `params` be a copy of the url search parameters provided - For each `element`: - If `element.type === 'checkbox'` - If `params` contains a parameter whose name is `element.name` and value is `element.value` - Set the element's checked state to true - Remove the param from the `params` list - Else set the element's checked state to false - If `element.type === 'select-multiple'`: - For each of `element`'s options: - If `params` contains a parameter whose name is `element.name` and value is `element.value` - Set the option's selected state to true - Remove the param from the `params` list - Else set the options's selected state to false - Else: - If `params` contains a parameter whose name is `element.name` - set `element.value` to that param's value - remove that param from the `params` list This algorithm should produce a form or fieldset that generates the passed-in query string when submitted, with caveats that hidden fields, submit buttons, mismatching numbers of parameters may produce a slight variation in data. ### Anything else? _No response_ -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/dom/issues/1402 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/dom/issues/1402@github.com>
Received on Monday, 1 September 2025 14:16:29 UTC