The FormData()
constructor creates a new FormData
object.
Note: This feature is available in Web Workers.
Syntax
var formData = new FormData(form)
Parameters
form
Optional- An HTML
<form>
element — when specified, theFormData
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.
Example
The following line creates an empty FormData
object:
var formData = new FormData(); // Currently empty
You could add a key/value pair to this using FormData.append
:
formData.append('username', 'Chris');
Or you can specify the optional form
argument when creating the FormData
object, to prepopulate it with values from the specified form:
<form id="myForm" name="myForm">
<div>
<label for="username">Enter name:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="useracc">Enter account number:</label>
<input type="text" id="useracc" name="useracc">
</div>
<div>
<label for="userfile">Upload file:</label>
<input type="file" id="userfile" name="userfile">
</div>
<input type="submit" value="Submit!">
</form>
Note: Only successful form controls are included in a FormData object, i.e. those with a name, not disabled and checked (radio buttons and checkboxes) or selected (one or more options within a select).
let myForm = document.getElementById('myForm');
let formData = new FormData(myForm);
Specifications
Specification | Status | Comment |
XMLHttpRequestThe definition of 'FormData()' in that specification. | Living Standard | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
FormData() constructor
|
Chrome
Full support 7 |
Edge
Full support 12 |
Firefox
Full support 4 |
IE
Full support 10 |
Opera
Full support 12 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 12 |
Safari iOS
Full support 5 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
See also
FormData() by Mozilla Contributors is licensed under CC-BY-SA 2.5.