Accessing FormData Values
FormData.get will do what you want in a small subset of browsers - look at the browser compatibility chart to see which ones (currently only Chrome 50+ and Firefox 39+). Given this form:
<form id="form">
<input name="inputTypeName">
</form>
You can access the value of that input via
var form = new FormData(document.getElementById("form"));
var inputValue = form.get("inputTypeName");
According to MDN:
An object implementing FormData can directly be used in a for...of structure, instead of entries(): for (var p of myFormData) is equivalent to for (var p of myFormData.entries())
Therefore you can access FormData values like that:
var formData = new FormData(myForm);
for (var p of formData) {
let name = p[0];
let value = p[1];
console.log(name, value)
}
First thing I don't think it's possible to build a FormData object from a form as you've specified, and to get values from the form use the method described in the accepted answer -- this is more of an addendum!
It looks like you can get some data out of a FormData object:
var formData = new FormData();
formData.append("email", "[email protected]");
formData.append("email", "[email protected]");
formData.get("email");
this will only return the first item for that key, in this case it will return '[email protected]', to get all the email addresses use the below code
formData.getAll("email")
Please see also: MDN article on formdata get method.
It seems you can't get values of the form element using FormData
.
The
FormData
object lets you compile a set of key/value pairs to send using XMLHttpRequest. Its primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data. The transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to "multipart/form-data".
However you can achieve it using simple Javascript like this
var formElements = document.forms['myform'].elements['inputTypeName'].value;