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 .

asked Sep 4, 2013 at 5:50 StuStirling StuStirling 16.1k 23 23 gold badges 95 95 silver badges 153 153 bronze badges

If by 'access' you meant visually inspect, try console.info(. myFormData); , which apparently casted the object to an array, at least in FF.

Commented Apr 17, 2023 at 21:18

15 Answers 15

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") 
8,098 6 6 gold badges 53 53 silver badges 75 75 bronze badges answered Apr 29, 2015 at 16:48 379 3 3 silver badges 6 6 bronze badges

FormData.get will do what you want and works in all browsers since around 2018-2019. Given this form:

You can access the value of that input via

var form = new FormData(document.getElementById("form")); var inputValue = form.get("inputTypeName"); 
788 8 8 silver badges 26 26 bronze badges answered Jun 5, 2016 at 20:36 6,769 2 2 gold badges 39 39 silver badges 42 42 bronze badges

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; 
answered Sep 4, 2013 at 5:59 56.3k 35 35 gold badges 135 135 silver badges 165 165 bronze badges Thats a shame. Would have been a lot nicer to grab them from the FormData. Thanks anyway. Commented Sep 4, 2013 at 6:01

This answer is no longer correct. As other answers point out, FormData can be iterated on with a for. of loop.

Commented Dec 31, 2020 at 10:03

Nowadays this is possible. Just scroll down a few answers to this updated solution that works now in 2022.

Commented May 27, 2022 at 22:49

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)
answered Apr 25, 2019 at 13:56 401 5 5 silver badges 12 12 bronze badges

This is a solution to retrieve the key-value pairs from the FormData:

var data = new FormData( document.getElementById('form') ); data = data.entries(); var obj = data.next(); var retrieved = <>; while(undefined !== obj.value) < retrieved[obj.value[0]] = obj.value[1]; obj = data.next(); >console.log('retrieved: ',retrieved); 
answered Mar 10, 2017 at 10:28 81 1 1 silver badge 1 1 bronze badge

Just to add to the previous solution from @Jeff Daze - you can use the FormData.getAll("key name") function to retrieve all of the data from the object.

336k 41 41 gold badges 315 315 silver badges 348 348 bronze badges answered Mar 3, 2016 at 17:46 Oculus Hut Oculus Hut 91 1 1 silver badge 5 5 bronze badges

My solution with for-of

const formData = new FormData(document.getElementById('form')) for (const [key, value] of formData)
answered May 20, 2020 at 19:50 SandroMarques SandroMarques 6,484 2 2 gold badges 46 46 silver badges 47 47 bronze badges

I came across an extremely simple way to convert form data into an object:

 const formData = new FormData(evt.currentTarget) const formObject = Object.fromEntries(formData.entries()) // <> 

Note that for multiple entries (for ex. checkbox); you need to handle separately with formData.getAll("favFruits") (for example)

const handleSubmit = (evt) => < evt.preventDefault() const formData = new FormData(evt.currentTarget) const formObject = Object.fromEntries(formData.entries()) // caveat for multiple values formObject.favFruits = formData.getAll("favFruits") console.log(formObject) >
 


answered May 9, 2023 at 5:37 23.3k 7 7 gold badges 105 105 silver badges 88 88 bronze badges

This is really cool because it's so minimalistic. Currently it also calls handleSubmit when user hits "Enter" in any text input. (I mean when I added input text boxes for my own use). I wonder if there's a way to suppress that so it only calls it when user clicks on the submit input (i.e., when button is pressed).

Commented Oct 20, 2023 at 23:18

HI @MarkSeagoe, glad you found the cool 😁. You can maybe use . This will immediately stop/suppress the native handler from getting triggered when user hits "Enter" in focused text-input on the form. Now, to handle the form submission on the click of this new button, you need to addEventListener("click". ) on the button and inside that handler, you need to get the from with document.getElementWithId("form-id") and use it to prepare your form-data.

Commented Oct 21, 2023 at 3:12

A one-liner to copy-paste: Object.fromEntries([. formData.keys()].map(key => [key, formData.getAll(key)])) . This will log everything in a dictionary, including array entries such as

Commented Jan 22 at 23:19
 // let form = document.querySelector("form"); const form = new FormData(); form.append("username", "Groucho"); form.append("accountnum", 123456); let data = new FormData(form) formObj = <>; for (var pair of data.entries()) < formObj[pair[0]] = pair[1] >console.log(formObj)
answered Nov 12, 2019 at 7:37 Selva Ganapathi Selva Ganapathi 1,036 11 11 silver badges 22 22 bronze badges Today it gets an Error. Commented Oct 20, 2023 at 22:46 check your form data structure Commented Oct 23, 2023 at 7:43 Selva I mean when I click "Run code snippet" here it gets "Uncaught TypeError". Commented Oct 23, 2023 at 11:28

If what you're trying to do is get the content of the FormData or append to it, this is what you should use:

Let's say you want to upload an image like so:

 uploadPic(e)> /> 

On change handler function:

const handleChange = e => < // Create a test FormData object since that's what is needed to save image in server let formData = new FormData(); //adds data to the form object formData.append('imageName', new Date.now()); formData.append('imageData', e.target.files[0]); >

append: adds an entry to the creates formData object where imageData is the key and e.target.files[0] is the property

You can now send this imageData object which contains data of the image to your server for processing

but to confirm if this formData has values a simple console.log(formData)/won't do it, what you should do is this:

//don't forget to add this code to your on change handler function for (var value of formData.values())

//I hope that explains my answer, it works for vanilla JavaScript and React.js. thanks in advance for your upvote

answered Feb 25, 2020 at 19:16 Chukwuemeka Maduekwe Chukwuemeka Maduekwe 8,098 6 6 gold badges 53 53 silver badges 75 75 bronze badges

Please edit your answer with textual information about what your code does and why it answers the question.

Commented Feb 25, 2020 at 22:10 I've done that, if that's what you need please don't forget to up vote Commented Feb 26, 2020 at 8:00

A simple HTML5 way (form to object) using the runarberg/formsquare library ( npm i --save formsquare ):

import formsquare from "formsquare"; const valuesObject = formsquare.filter((el) => !el.disabled).parse(document.getElementById('myForm')); //=>
answered Feb 8, 2020 at 18:16 David DIVERRES David DIVERRES 1,908 21 21 silver badges 32 32 bronze badges

Creating a FormData object in JavaScript is quite straightforward and it's a useful tool for working with HTML forms. Here's how you can create a FormData object from an HTML form:

Assuming you have an HTML form with the id "myForm"

const formElement = document.getElementById('myForm'); const formData = new FormData(formElement); 

The FormData constructor takes a reference to your HTML form element as an argument, and it automatically collects all the form fields and their values.

Once you have the formData object, you can access and manipulate the form data easily. For example, you can append additional data to it or send it via an AJAX request.

While the FormData object itself may not be as well-documented as other JavaScript features, it's part of the HTML Living Standard,