The FormData()
constructor creates a new FormData
object.
Note: This feature is available in Web Workers.
var formData = new FormData(form)
form
Optional
<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.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: All input elements have a 'name' attribute. It is necessary to be able to access their values later.
var myForm = document.getElementById('myForm'); formData = new FormData(myForm);
Specification | Status | Comment |
---|---|---|
XMLHttpRequest The definition of 'FormData()' in that specification. | Living Standard | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 7 | Yes | 4 | 10 | 12 | 5 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | 12 | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData