Collect named controls with FormData
Quick answer
new FormData(form) constructs entries from successful form controls. Entries are ordered and may have repeated names. Disabled controls, unchecked checkboxes and controls without names are normally omitted. Use get for the first value, getAll for repeated values and entries for traversal. Values can be strings or File objects; a plain object conversion can discard repeated entries.
Browser examples
Run each snippet in a fresh browser document. These are static code blocks; ByteMind browser execution is not verified. Storage examples require a test origin.
const f=document.createElement("form");for(const [name,value,disabled] of [["item","pen",false],["hiddenField","x",true],["","ignored",false]]){const i=document.createElement("input");i.name=name;i.value=value;i.disabled=disabled;f.append(i);}console.log(JSON.stringify([...new FormData(f)]));Expected console output:
[["item","pen"]]Disabled and unnamed controls are excluded from this entry list.
const d=new FormData();d.append("tag","a");d.append("tag","b");console.log(d.get("tag"),JSON.stringify(d.getAll("tag")),JSON.stringify(d.getAll("missing")));Expected console output:
a ["a","b"] []get returns the first value; getAll returns all matches.
Common mistake
Do not assume an input id supplies its name for submission. Avoid converting repeated fields to a plain object unless collapsing them is intentional.
Practice
Build a small preferences form with two checked checkboxes sharing a name and a disabled field; inspect all submitted entries without sending data.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Collect named controls with FormData — Knowledge Check
Apply the concept and predict the browser example behavior.
Which attribute identifies a normal form field in FormData?
Which method preserves all repeated values for a name?
Does constructing FormData automatically send data to a server?
What is the exact console output in Collect named controls with FormData example 1?
const f=document.createElement("form");for(const [name,value,disabled] of [["item","pen",false],["hiddenField","x",true],["","ignored",false]]){const i=document.createElement("input");i.name=name;i.value=value;i.disabled=disabled;f.append(i);}console.log(JSON.stringify([...new FormData(f)]));What is the exact console output in Collect named controls with FormData example 2?
const d=new FormData();d.append("tag","a");d.append("tag","b");console.log(d.get("tag"),JSON.stringify(d.getAll("tag")),JSON.stringify(d.getAll("missing")));