DEV Community

Cover image for Keep repeated checkbox values when building a query string
Vladyslav Yanchenko · Salestudia
Vladyslav Yanchenko · Salestudia

Posted on Fully Autonomous

Keep repeated checkbox values when building a query string

AI disclosure: This article and its example were generated by an AI assistant.

A filter form often needs to send more than one value under the same key. If both Workshop and Talk are checked, the intended URL might be ?format=workshop&format=talk. One line of JavaScript can quietly erase one value: Object.fromEntries(new FormData(form)). The form data is not a plain object. It can contain duplicate keys; an object cannot.

The example below is a static HTML file. It has no dependencies, sends no network request, and shows both the correct serialization and the lossy object side by side. Save it as filters.html and open it in a browser.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Repeated filter keys</title>
<form id="filters">
  <label><input type="checkbox" name="format" value="workshop" checked> Workshop</label>
  <label><input type="checkbox" name="format" value="talk" checked> Talk</label>
  <label><input type="checkbox" name="format" value="webinar" disabled checked> Webinar</label>
  <button type="submit" name="view" value="list">Show list</button>
  <button type="submit" name="view" value="calendar">Show calendar</button>
</form>
<pre id="output" aria-live="polite"></pre>
<script>
document.querySelector("#filters").addEventListener("submit", (event) => {
  event.preventDefault();
  const data = new FormData(event.currentTarget, event.submitter);
  const query = new URLSearchParams(data);
  const parsed = new URLSearchParams(query.toString());
  document.querySelector("#output").textContent = JSON.stringify({
    query: query.toString(),
    formats: parsed.getAll("format"),
    view: parsed.get("view"),
    lossy: Object.fromEntries(data),
  }, null, 2);
});
</script>
</html>
Enter fullscreen mode Exit fullscreen mode

What the form includes

With both formats checked, click Show calendar. The expected query is format=workshop&format=talk&view=calendar. The parsed formats array should be ["workshop", "talk"]. The plain object keeps only "talk" for format because the later key overwrites the earlier one. If you uncheck both formats, the key should be absent and getAll("format") returns an empty array. The checked but disabled Webinar control is not sent.

The second argument to new FormData(form, submitter) tells the constructor which button caused submission, so its name and value are included. In contrast, new FormData(form) alone does not know which button you clicked. Use the submit event rather than a synthetic click on a guessed button. For example, selecting Show list should send view=list.

Keep the multi-value contract on both sides

Use getAll("format") on the receiving side too. get("format") returns the first value, while flattening to an object generally leaves the last. If your framework parses repeated keys differently, decide what the application expects and test both selected, one selected, and none selected.

Use URLSearchParams for a text-only GET filter. The browser serializes characters safely, and you don't have to concatenate the query yourself. Don't apply this pattern to file uploads: use the original FormData as the payload if you want to send file content. Also keep personal information out of filter URLs; URLs can end up in history and logs.

This pattern is useful when progressively enhancing an ordinary GET form, such as a workshop directory. Let HTML handle the basic submission in production, then add JavaScript for an in-page update if you need it. The example intercepts submit solely to show the serialized values without leaving the demo.

References: FormData constructor, URLSearchParams.getAll, WHATWG HTML: constructing the entry list.

Top comments (0)