I have a FormData object which I create in javascript
from an HTML
form like so. The FormData
object doesn't seem very well documented (it may just be me searching the wrong things!).
var form = new FormData(document.getElementById("form"));
My Question
How do I access the different input values of this FormData
object before I send it off? Eg. form.name
accesses the value that was entered into the input with the name form.name
.
The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead. Note: This method is available in Web Workers.
Grabbing data from a FormData object If you want to snitch into a FormData object visit the example HTML form in a browser and place a breakpoint on console. log(event. formData) . Fill and submit the form with the browser's console opened and save the object as a global variable.
FormData.get() Returns the first value associated with a given key from within a FormData object. FormData.getAll() Returns an array of all the values associated with a given key from within a FormData .
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With