7

Am creating a formData object using form id and was doing the following:

var formDataDetails = new FormData(document.getElementById("form_id"));
for (var entry of formDataDetails{
     res[entry[0]] = entry[1];
}

Am later doing JSON stringify and doing POST.

But I have found out recently that for..of loop is not supported in 'Internet Explorer' yet. And I believe using for..in loop is not correct since it is used to iterate through enumerable objects (loop through properties of an object rather).

How should I go about iterating through formData, for Internet Explorer?

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
  • 1
    No answers yet. What would be the best way to capture form details and do POST, so that the code works also in IE? – Anuj Balan Aug 01 '17 at 03:07
  • I am facing the exactly same issue. I am still trying to deal with.. A headache is coming. – Delphine Jan 23 '18 at 16:04
  • Possible duplicate of [Iterating through FormData in IE](https://stackoverflow.com/questions/37938955/iterating-through-formdata-in-ie) This has information on a polyfill for this – Mark Schultheiss Sep 07 '19 at 13:21

1 Answers1

0

I am finding that, as of this post, IE still not working reliably in for..of loop through FormData objects. So, my solution, just avoid FormData when you will need to iterate the collection. FormData works fine in IE if you are just using it to post form data.

If you need to iterate the values of the form before they are sent, you could do as I do -- just work directly with the form.elements collection.

Something like this:

export function form2Obj(f) {
   var elemArray = f.elements;
   var formObj = {};
   for (var k in elemArray) {
      var input = elemArray[k];
      if (!input || !input.name || !input.value) continue;
      formObj[input.name] = input.value;  
      // etc, need special handling for inputs of type radio 
      // checkbox, textarea, and select most likely
   }
   return formObj;

}

For the record, I am using webpack to compile to ES6. When I compile in dev mode IE can handle the for..of loop. When I compile in production mode IE does not work.

jomofrodo
  • 1,119
  • 11
  • 19
  • 1
    The funny thing is, every time I open IE to test my app, it still is asking me "Do you want to make IE your default browser?". Hilarious. – jomofrodo Feb 07 '18 at 22:46