The FormData API
Collect form data programmatically with the FormData interface.
What FormData Is
FormData is a built-in object representing form fields and their values, suitable for direct submission via fetch or XMLHttpRequest. It handles file uploads, multipart encoding, and arbitrary key-value pairs without manual serialization.
Construct From a Form
Pass a <form> element to the constructor: const data = new FormData(formElement). The result contains every successful form control (named inputs, selects, textareas, checked checkboxes, files). Disabled and unchecked controls are excluded automatically.
document.forms.checkout.addEventListener("submit", async (e) => {
e.preventDefault();
const data = new FormData(e.target);
await fetch("/api/checkout", { method: "POST", body: data });
});