Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  161] [ 1]  / answers: 1 / hits: 139265  / 10 Years ago, wed, july 30, 2014, 12:00:00

I'm trying to get a set of keys (input names or similar) and values (input values) from a web form:



<body>
<form>
<input type=text name=banana value=swag>
</form>

<script>
var form = document.querySelector('form');
var formData = new FormData(form);
</script>
</body>


According to the FormData documentation, formData should contain the keys and values from the form. But console.log(formData) shows formData is empty.



How can I quickly get the data from the form using FormData?



JSFiddle


More From » form-data

 Answers
13

Update: the XHR spec now includes several more functions to inspect FormData objects.



FireFox has supported the newer functions since v39.0, Chrome is slated to get support in v50. Not sure about other browsers.



var form = document.querySelector('form');
var formData = new FormData(form);

for (var [key, value] of formData.entries()) {
console.log(key, value);
}

//or

console.log(...formData)

[#69993] Monday, July 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;