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.
new FormData(form)
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);
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
|
© 2005–2021 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