The FormData()
constructor creates a new FormData
object.
Note: This feature is available in Web Workers.
The FormData()
constructor creates a new FormData
object.
Note: This feature is available in Web Workers.
js
new FormData()
new FormData(form)
new FormData(form, submitter)
form
Optional
An HTML <form>
element — when specified, the FormData
object will be populated with the form
's current keys/values using the name property of each element for the keys and their submitted value for the values. It will also encode file input content.
submitter
Optional
A submit button that is a member of the form
. If the submitter
has a name
attribute or is an <input type="image">
, its data will be included in the FormData
object (e.g. btnName=btnValue
).
TypeError
Thrown if the specified submitter
is not a submit button.
NotFoundError
DOMException
Thrown if the specified submitter
isn't a member of the form
. The submitter
must be either a descendant of the form element or must have a form
attribute referring to the form.
The following line creates an empty FormData
object:
js
const formData = new FormData();
You could add a key/value pair to this using append()
:
js
formData.append("username", "Chris");
You can specify the optional form
and submitter
arguments when creating the FormData
object, to prepopulate it with values from the specified form.
Note: Only successful form controls are included in a FormData object, i.e. those with a name and not in a disabled state.
html
<form id="form">
<input type="text" name="text1" value="foo" />
<input type="text" name="text2" value="bar" />
<input type="text" name="text2" value="baz" />
<input type="checkbox" name="check" checked disabled />
<button name="intent" value="save">Save</button>
<button name="intent" value="saveAsCopy">Save As Copy</button>
</form>
<output id="output"></output>
js
const form = document.getElementById("form");
const submitter = document.querySelector("button[value=save]");
const formData = new FormData(form, submitter);
const output = document.getElementById("output");
for (const [key, value] of formData) {
output.textContent += `${key}: ${value}\n`;
}
For brevity, the <form>
element is hidden from view.
Specification |
---|
XMLHttpRequest Standard # dom-formdata |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
FormData |
5 | 12 | 4 | 10 | 12 | 5 | 3 | 18 | 4 | 12 | 5 | 1.0 |
submitter |
112 | 112 | 111 | No | 98 | 16.4 | 112 | 112 | 111 | No | 16.4 | No |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData