I'm using FormData to send information back to the server. In some cases however I need to read out the data before I send it.
Chrome allows you to iterate through the collection but IE does not supply the same methods.
The code below works in Chrome:
// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// Display the key/value pairs
for(var pair of formData.entries()) {
console.log(pair[0]+ ', '+ pair[1]);
}
JS Fiddle
Does anyone know how to achieve the same result in IE? Thanks.
getAll() The getAll() method of the FormData interface returns all the values associated with a given key from within a FormData object. Note: This method is available in Web Workers.
I think it's also useful to note that you can grab an existing form's data using: var formData = new FormData(formElement) . If you are using JQuery, you can therefore do this: var formData = new FormData($('#formElementID')[0]) . Then append data as needed. Still, this has to be one of the worst browser APIs.
get() 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.
There's a FormData
polyfill that works. For details, read their docs.
To make it work with IE 10 and above, you'll just have to add a WeakMap
polyfill as well.
JSBin demo for IE10 and above.
<script src="https://unpkg.com/weakmap-polyfill/weakmap-polyfill.min.js"></script>
<script src="https://unpkg.com/formdata-polyfill"></script>
<form action="" id="f">
<input type="text" name="i1" value="v1">
<input type="text" name="i2" value="v2">
</form>
<script type="text/javascript">
console.clear();
// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// Display the key/value pairs
var formDataEntries = formData.entries(), formDataEntry = formDataEntries.next(), pair;
while (!formDataEntry.done) {
pair = formDataEntry.value;
console.log(pair[0] + ', ' + pair[1]);
formDataEntry = formDataEntries.next();
}
// or, if you are really into compact code
var es, e, pair;
for (es = formData.entries(); !(e = es.next()).done && (pair = e.value);) {
console.log(pair[0] + ', ' + pair[1]);
}
// testing getting from form
var myForm = document.getElementById('f');
for (es = new FormData(myForm).entries(); !(e = es.next()).done && (pair = e.value);) {
console.log(pair[0] + ', ' + pair[1]);
}
</script>
Code above picks up the latest versions. Versions tested: https://unpkg.com/[email protected]/weakmap-polyfill.min.js
and https://unpkg.com/[email protected]/formdata.min.js
If you only need IE 11 and above, you can remove the WeakMap
's polyfill and just keep FormData
's.
JSBin demo here.
<script src="https://unpkg.com/formdata-polyfill"></script>
<form action="" id="f">
<input type="text" name="i1" value="v1">
<input type="text" name="i2" value="v2">
</form>
<script type="text/javascript">
console.clear();
// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// Display the key/value pairs
var formDataEntries = formData.entries(), formDataEntry = formDataEntries.next(), pair;
while (!formDataEntry.done) {
pair = formDataEntry.value;
console.log(pair[0] + ', ' + pair[1]);
formDataEntry = formDataEntries.next();
}
// or, if you are really into compact code
var es, e, pair;
for (es = formData.entries(); !(e = es.next()).done && (pair = e.value);) {
console.log(pair[0] + ', ' + pair[1]);
}
// testing getting from form element
const myForm = document.getElementById('f');
for (es = new FormData(myForm).entries(); !(e = es.next()).done && (pair = e.value);) {
console.log(pair[0] + ', ' + pair[1]);
}
</script>
Code above picks up the latest version. Version tested: https://unpkg.com/[email protected]/formdata.min.js
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