Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing FormData Values

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.

like image 837
StuStirling Avatar asked Sep 04 '13 05:09

StuStirling


People also ask

How can I access data in FormData?

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.

How do I get data from form event?

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.

What does FormData return?

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 .


1 Answers

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; 
like image 105
Praveen Avatar answered Oct 23 '22 11:10

Praveen